请各位高手帮帮忙,编个JAVA程序(急)!!!!

2024-12-03 06:32:43
推荐回答(1个)
回答1:

//主类
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.BevelBorder;

public class game extends JFrame {
private static final long serialVersionUID = 342676014673887411L;
private myLabel[] mylabel = new myLabel[12];// 按钮
private Point[] p = new Point[12];// 按钮位置
private myLabel labelTemp;// 上一个被选择的按钮
private static int count;// 总共按了多少下

public game() {
getContentPane().setLayout(new GridLayout(0, 3));
for (int i = 0; i < mylabel.length; i++) {
p[i] = new Point();
if (i < 3) {
p[i].y = 0;
p[i].x = i;
} else if (i < 6) {
p[i].y = 1;
p[i].x = i - 3;
} else if (i < 9) {
p[i].y = 2;
p[i].x = i - 6;
} else if (i < 12) {
p[i].y = 3;
p[i].x = i - 9;
}
mylabel[i] = new myLabel();
mylabel[i].setHorizontalAlignment(JLabel.CENTER);
mylabel[i].setBorder(new BevelBorder(BevelBorder.LOWERED));
mylabel[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clicked(e);
}

});
mylabel[i].setP(p[i]);
getContentPane().add(mylabel[i]);

}
randInitText();
for (int i = 0; i < 12; i++) {
if (mylabel[i].getText().equals("") || mylabel[i].getText() == null) {
labelTemp = mylabel[i];
mylabel[i].setBackground(Color.BLUE);
}
}
this.setBounds(140, 150, 260, 400);
this.setVisible(true);
}

/*
* 初始化按钮的值
*/
public void randInitText() {
Set set = new HashSet();
while (true) {
int temp = randNumber();
set.add(temp);
if (set.size() == 12) {
break;
}
}
int a = 0;
Iterator it = set.iterator();
while (it.hasNext()) {
Integer temp = (Integer) it.next();
if (temp != 12)
mylabel[a].setText("" + temp);
a++;
}
}

/*
* 返回一个1-12的随机数字
*/
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 12 + 1);
}

/*
* 鼠标单击事件
*/
public void Clicked(ActionEvent e) {
myLabel tp = (myLabel) e.getSource();
Point pp = tp.getP();
if (labelTemp != null) {
String temp = tp.getText();
if ((labelTemp.X() == pp.x)
&& ((Math.abs(labelTemp.Y() - pp.y) == 1))) {
labelTemp.setBackground(Color.WHITE);
tp.setBackground(Color.BLUE);
tp.setText(labelTemp.getText());
labelTemp.setText(temp);
count++;
} else if ((labelTemp.Y() == pp.y)
&& ((Math.abs(labelTemp.X() - pp.x) == 1))) {
labelTemp.setBackground(Color.WHITE);
tp.setBackground(Color.BLUE);
tp.setText(labelTemp.getText());
labelTemp.setText(temp);
count++;
} else {
return;
}

} else {
tp.setBackground(Color.BLUE);
}
if (this.isWin()) {
JOptionPane.showMessageDialog(this, "恭喜你赢了,总共用了:" + count + "步");
count = 0;
randInitText();
return;
}
labelTemp = (myLabel) e.getSource();
}

/*
* 判断是否赢了
*/
public boolean isWin() {
for (int i = 0; i < 11; i++) {
if (!mylabel[i].getText().equals(("" + (i + 1)).trim())) {
return false;
}
}
return true;
}

public static void main(String[] args) {
new game().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//按钮类
import java.awt.Color;
import java.awt.Point;
import javax.swing.JButton;

public class myLabel extends JButton {
private Point p;
public myLabel() {
this.setBackground(Color.WHITE);
}
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public int X() {
return p.x;
}
public int Y() {
return p.y;
}
}