package PinTu;import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;public class MyPintu implements ActionListener
{ private JButton b00, b01, b02, b10, b11, b12, b20, b21, b22; private JFrame frame; private ImageIcon[][] icon = new ImageIcon[3][3];
private ImageIcon Icon = new ImageIcon("");
private int[][] picture = new int[3][3];
private int[] array; private Random random; private boolean bol; public void play()  //生成0-8范围不同的随机数保存到数组当中
{
List<Integer> list = new ArrayList<Integer>(); while (true)
{
int num = (int) (Math.random() * 9);
boolean isHave = false;
for (int j = 0; j < list.size(); j++)
{
if (num == list.get(j))
{
isHave = true;
break;
}
}
if (!isHave)
{
list.add(num);
} if (list.size() == 9)
{
break;
}
}
int[] arr = new int[9];
int t = 0;
while (t < 9)
{
for (Iterator iter = list.iterator(); iter.hasNext();)
{
String value = iter.next().toString();
int a = Integer.parseInt(value);
arr[t++] = a;
}
}
t = 0;
for (int i = 0; i < picture.length; i++)
{
for (int j = 0; j < picture[i].length; j++)
{
picture[i][j] = arr[t++];
}
}
for (int i = 0; i < picture.length; i++)
{
for (int j = 0; j < picture[i].length; j++)
{
icon[i][j] = new ImageIcon("src/picture/" + picture[i][j]
+ ".jpg");
}
}
b00.setIcon(icon[0][0]);
b01.setIcon(icon[0][1]);
b02.setIcon(icon[0][2]);
b10.setIcon(icon[1][0]);
b11.setIcon(icon[1][1]);
b12.setIcon(icon[1][2]);
b20.setIcon(icon[2][0]);
b21.setIcon(icon[2][1]);
b22.setIcon(icon[2][2]);
} public MyPintu()
{
frame = new JFrame("拼图游戏");
b00 = new JButton();
b00.addActionListener(this);
b01 = new JButton();
b01.addActionListener(this); b02 = new JButton();
b02.addActionListener(this); b10 = new JButton();
b10.addActionListener(this); b11 = new JButton();
b11.addActionListener(this); b12 = new JButton();
b12.addActionListener(this); b20 = new JButton();
b20.addActionListener(this); b21 = new JButton();
b21.addActionListener(this); b22 = new JButton();
b22.addActionListener(this); this.init();
} public void init()
{
frame.setLayout(new GridLayout(3, 3));
frame.add(b00);
frame.add(b01);
frame.add(b02);
frame.add(b10);
frame.add(b11);
frame.add(b12);
frame.add(b20);
frame.add(b21);
frame.add(b22);
        frame.setSize(300, 300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public static void main(String[] args)
{
MyPintu m = new MyPintu();
m.play();
} public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b00)
{
bol = false;
for (int i = 0; i < picture.length; i++)
{ for (int j = 0; j < picture[i].length; j++)
{
if (picture[i][j] == 0)
{
int n;
n = picture[i][j];
picture[i][j] = picture[0][0];
picture[0][0] = n;
ImageIcon icon1 = new ImageIcon("src/picture/"
+ picture[i][j] + ".jpg");
b00.setIcon(icon1);
ImageIcon icon2 = new ImageIcon("src/picture/"
+ picture[0][0] + ".jpg");
 /*
  * 一旦第i个==0 那么就进行替换按钮背景  在这里下面写不下去了 !!!!谁麻烦帮我修改下
  
  * */
bol = true;
break;
}
}
if (bol == true)
{
break;
}
}
}
}
}