1、package love;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.*;public class Games extends JFrame implements ActionListener private JButton buttons;/ 存储按键的数组private Container container;/ 一个容器private GridLayout layout;
2、/ 布局方式为 GridLayoutprivate int count = 0, is = new int8;/ count的作用是计算按键移动的次数,is储存一个随机产生的 1到 8数字数组public Games() super(“拼图游戏“);/ 设置标题layout = new GridLayout(3, 3);/ 3行 3列container = getContentPane();/ 该容器的布局方式,及其重要,否则产生空引用异常container.setLayout(layout);/ 将该布局方式作用于容器buttons = new JButton33;/ 给按键数组分配储存空间
3、int locate1, locate2;/ locate1用来指示当前产生的元素 locate2用来指示locate1之前的元素for (locate1 = 0; locate1 8; locate1+) / 该方法作用是产生 1到 8这 8个数,随机分配给数组,即无序排列int g = new Random().nextInt(8) + 1;/ 随机产生一个空白按键,即不显示的那个islocate1 = g;for (locate2 = locate1 - 1; 0 = locate2; locate2-) if (islocate1 = islocate2)break;if (locat
4、e2 != -1)locate1-;int temp = 0;int r = new Random().nextInt(3);/ 随机产生一个 0到 3的数,代表空白按键的行int l = new Random().nextInt(3);/ 随机产生一个 0到 3的数,代表空白按键的列for (int i = 0; i 3; i+) for (int j = 0; j 3; j+) if (r = i container.add(buttonsij);buttonsij.setVisible(false); else buttonsij = new JButton(“ + istemp+);/
5、 将数组数组的值作为显示的8个按键的名字container.add(buttonsij);buttonsij.addActionListener(this);/ 注册监听事件setSize(300, 300);/ 设置显示容器区域大小setVisible(true);public void actionPerformed(ActionEvent event) / 事件处理,由于计算量小该处将所有按键单独分析JButton b = (JButton) event.getSource();if (b = buttons00) if (buttons01.getText().equals(“9“)
6、buttons01.setText(“ + buttons00.getText();buttons01.setVisible(true);buttons00.setText(“9“);buttons00.setVisible(false);count+; else if (buttons10.getText().equals(“9“) buttons10.setText(“ + buttons00.getText();buttons10.setVisible(true);buttons00.setText(“9“);buttons00.setVisible(false);count+;else
7、 if (b = buttons01) if (buttons00.getText().equals(“9“) buttons00.setText(“ + buttons01.getText();buttons00.setVisible(true);buttons01.setText(“9“);buttons01.setVisible(false);count+; else if (buttons02.getText().equals(“9“) buttons02.setText(“ + buttons01.getText();buttons02.setVisible(true);button
8、s01.setText(“9“);buttons01.setVisible(false);count+; else if (buttons11.getText().equals(“9“) buttons11.setText(“ + buttons01.getText();buttons11.setVisible(true);buttons01.setText(“9“);buttons01.setVisible(false);count+; else if (b = buttons02) if (buttons01.getText().equals(“9“) buttons01.setText(
9、“ + buttons02.getText();buttons01.setVisible(true);buttons02.setText(“9“);buttons02.setVisible(false);count+; else if (buttons12.getText().equals(“9“) buttons12.setText(“ + buttons02.getText();buttons12.setVisible(true);buttons02.setText(“9“);buttons02.setVisible(false);count+; else if (b = buttons1
10、0) if (buttons00.getText().equals(“9“) buttons00.setText(“ + buttons10.getText();buttons00.setVisible(true);buttons10.setText(“9“);buttons10.setVisible(false);count+; else if (buttons11.getText().equals(“9“) buttons11.setText(“ + buttons10.getText();buttons11.setVisible(true);buttons10.setText(“9“);
11、buttons10.setVisible(false);count+; else if (buttons20.getText().equals(“9“) buttons20.setText(“ + buttons10.getText();buttons20.setVisible(true);buttons10.setText(“9“);buttons10.setVisible(false);count+; else if (b = buttons11) if (buttons01.getText().equals(“9“) buttons01.setText(“ + buttons11.get
12、Text();buttons01.setVisible(true);buttons11.setText(“9“);buttons11.setVisible(false);count+; else if (buttons10.getText().equals(“9“) buttons10.setText(“ + buttons11.getText();buttons10.setVisible(true);buttons11.setText(“9“);buttons11.setVisible(false);count+; else if (buttons12.getText().equals(“9
13、“) buttons12.setText(“ + buttons11.getText();buttons12.setVisible(true);buttons11.setText(“9“);buttons11.setVisible(false);count+; else if (buttons21.getText().equals(“9“) buttons21.setText(“ + buttons11.getText();buttons21.setVisible(true);buttons11.setText(“9“);buttons11.setVisible(false);count+;
14、else if (b = buttons12) if (buttons02.getText().equals(“9“) buttons02.setText(“ + buttons12.getText();buttons02.setVisible(true);buttons12.setText(“9“);buttons12.setVisible(false);count+; else if (buttons22.getText().equals(“9“) buttons22.setText(“ + buttons12.getText();buttons22.setVisible(true);bu
15、ttons12.setText(“9“);buttons12.setVisible(false);count+; else if (buttons11.getText().equals(“9“) buttons11.setText(“ + buttons12.getText();buttons11.setVisible(true);buttons12.setText(“9“);buttons12.setVisible(false);count+; else if (b = buttons20) if (buttons10.getText().equals(“9“) buttons10.setT
16、ext(“ + buttons20.getText();buttons10.setVisible(true);buttons20.setText(“9“);buttons20.setVisible(false);count+; else if (buttons21.getText().equals(“9“) buttons21.setText(“ + buttons20.getText();buttons21.setVisible(true);buttons20.setText(“9“);buttons20.setVisible(false);count+;else if (b = butto
17、ns21) if (buttons20.getText().equals(“9“) buttons20.setText(“ + buttons21.getText();buttons20.setVisible(true);buttons21.setText(“9“);buttons21.setVisible(false);count+; else if (buttons22.getText().equals(“9“) buttons22.setText(“ + buttons21.getText();buttons22.setVisible(true);buttons21.setText(“9
18、“);buttons21.setVisible(false);count+; else if (buttons11.getText().equals(“9“) buttons11.setText(“ + buttons21.getText();buttons11.setVisible(true);buttons21.setText(“9“);buttons21.setVisible(false);count+; else if (b = buttons22) if (buttons21.getText().equals(“9“) buttons21.setText(“ + buttons22.
19、getText();buttons21.setVisible(true);buttons22.setText(“9“);buttons22.setVisible(false);count+; else if (buttons12.getText().equals(“9“) buttons12.setText(“ + buttons22.getText();buttons12.setVisible(true);buttons22.setText(“9“);buttons22.setVisible(false);count+;if (buttons00.getText().equals(“1“)/ 假如 1到 8已经排列完成,则执行该语句/ 弹出对话框,显示共移动步数System.exit(0);/ 退出程序public static void main(String args) Games game = new Games();game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);