package love;
import .*;
import ;
import ;
import ;
import .*;
public class Games extends JFrame implements ActionListener {
private JButton buttons[][];// 存储按键的数组
private Container container;// 一个容器
private GridLayout layout;// 布局方式为GridLayout
private int count = 0, is[] = new int[8];// count的作用是计算按键移动的次数,is[]储存一个随机产生的1到8数字数组
public Games() {
super("拼图游戏");// 设置标题
layout = new GridLayout(3, 3);// 3行3列
container = getContentPane();// 该容器的布局方式,及其重要,否则产生空引用异常
(layout);// 将该布局方式作用于容器
buttons = new JButton[3][3];// 给按键数组分配储存空间
int locate1, locate2;// locate1用来指示当前产生的元素 locate2用来指示locate1之前的元素
for (locate1 = 0; locate1 < 8; locate1++) {// 该方法作用是产生1到8这8个数,随机分配给数组,即无序排列
int g = new Random().nextInt(8) + 1;// 随机产生一个空白按键,即不显示的那个
is[locate1] = g;
for (locate2 = locate1 - 1; 0 <= locate2; locate2--) {
if (is[locate1] == is[locate2])
break;
}
if (locate2 != -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 && l == j) {// 空白按键设置为9,不显示
buttons[i][j] = new JButton("9");
(buttons[i][j]);
buttons[i][j].setVisible(false);
} else {
buttons[i][j] = new
java课程设计-拼图游戏代码 来自淘豆网m.daumloan.com转载请标明出处.