把以下代码改为此思路:整个界面是一个JFrame类的子类;
整个窗口的Layout是BorderLayout;
有两个JPanel,中间的数字按钮部分放在一个Grid型layout的JPanel中;
开始游戏按钮放在一个JPanel中;
中间数字部分每一个格子都是一个按钮;
需要一个辅助数组int[] num,存储16个整数值,对应16个数值按钮上的显示的数值;
需要一个辅助数组JButtonbtn[],这个数组用于存储界面上的16个button;button上显示的值为16的button设置成setVisible(false),即不可见,表示为空白块;
开始游戏按钮的事件处理:
开始游戏按钮的功能是打乱16个按钮的显示数字的顺序;
num数组用于存储按钮上的数字,所以只要把num数组中的数字打乱即可;
然后把打乱之后的num数组的值显示到对应的按钮上;
打乱的方法可以是产生2个随机数,作为num数组的两个下标,把这两个下标对应的数字交换。重复多次,比如500次;
把显示的值为16的按钮设置成不可见,即空白块。
每一个数字按钮的点击的事件处理
点击一个按钮A之后,检查这个按钮的上下左右是不是有空白块(换句话说,空白块是不是在A的上下左右);
如果按钮A周围有空白块,则交换空白块和按钮A;
交换两个按钮的做法为:
改变num数组,将空白块的数字和按钮A的数字交换;
两个按钮的可见性,将新的空白块,也就是按钮A设置成隐藏,老的空白块设置成可见;
更新两个按钮显示的值;
焦点(Focus)移动到原来的按钮A上,方便用户看清;
如果按钮A周围没有空白块,不处理;
检查是否已经排序好了,如果排序好,提示用户游戏成功。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SortGame extends JFrame implements ActionListener {
private static final long serialVersionUID = -3252810413692236650L;
private JPanel mainPanel = new JPanel();
private JButton button[] = new JButton[16];
private int butNum[] = new int[16];
private Vector<String> optionalNum = new Vector<String>();
public SortGame(){
super("排块游戏");
mainPanel.setLayout(new GridLayout(4,4,3,3));
getContentPane().add(mainPanel);
for(int i=0; i<16; i++) {
button[i] = new JButton();
button[i].addActionListener(this);
mainPanel.add(button[i]);
butNum[i] = 0;
}
initButtonNum();
}//SortGame
private void initButtonNum() {
for(int i=0; i<16; i++) {
optionalNum.add(String.valueOf(i));
}
int index = -1;
String str = null;
for(int i=0; i<16; i++){
index = (int)(Math.random()*optionalNum.size());
str = optionalNum.get(index);
if(str.equals("0")){
button[i].setText("");
butNum[i] = 0;
}
else {
button[i].setText(str);
butNum[i] = Integer.parseInt(str) ;
}
optionalNum.remove(index);
}
}//initButtonNum
public void actionPerformed(ActionEvent e) {
int num =-1, location = -1;
int aim = -1;
int i=0;
if(e.getActionCommand().length() == 0)
return;
num = Integer.parseInt(e.getActionCommand());
while(i<16){
if(num == butNum[i++]){
location = i-1;
break;
}
}
int candidates[] = {location-1, location+1, location-4, location+4};
for(int j=0; j<4; j++){
if(check(candidates[j]))
aim = candidates[j];
}
if(aim >= 0 && aim < 16){
String tempStr = button[location].getText();
int tempNum = butNum[location];
button[location].setText("");
butNum[location] = 0;
button[aim].setText(tempStr);
butNum[aim] = tempNum;
}
if(evaluate()){
int choice = JOptionPane.showConfirmDialog(this, "你赢了![请点击开始]再来一次.",null,JOptionPane.YES_OPTION);
if(choice == JOptionPane.YES_OPTION)
initButtonNum();
}
}
private boolean check(int index) {
if(index >= 0 && index < 16 && butNum[index] == 0)
return true;
else
return false;
}
private boolean evaluate() {
int i,j;
for(i=0; i<16; i++){
if(butNum[i] != i)
break;
}
if(i == 16) return true;
for(i=0; i<15; i++){
if(butNum[i] != i+1)
break;
}
if(i == 15) return true;
for(i=0,j=15; j>=0; i++,j--){
if(butNum[j] != i)
break;
}
if(i == 16) return true;
for(i=0,j=15; j>0; i++,j--){
if(butNum[j] != i+1)
break;
}
if(i == 15) return true;
return false;
}
public static void main(String[] args) {
SortGame game = new SortGame();
game.setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setSize(300,300);
game.setVisible(true);
}
}