GUI练手拼图游戏思路:
1、分析窗口所需要的组件
2、按一定的顺序将组件填入窗口中 (注意要用Jpanel)
所用类:JFrame, JLabel,JPanel,JButton
3、利用二维数组思想
4、给按钮创建事件
5、逐步实现其功能
package com.D_02;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureFrame extends JFrame {
//定义二维数组来存储游戏编号
private int[][] datas={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,0}
};
//定义成功的数组
private int[][] windatas={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{12,14,15,0}
};
public PictureFrame(){
//初始化窗口
initFrame();
//打乱二维数组
RandomDates();
//绘制窗体
paintView();
//给按钮添加事件
addButtonEvent();
//设置窗口可见
this.setVisible(true);
}
//定义上下左右,求助,重置按钮
private JButton shangButton;
private JButton zuoButton;
private JButton xiaButton;
private JButton youButton;
private JButton qiuzhuButton;
private JButton chongzhiButton;
//定义面板
JPanel imagePanel;
//空白0号图片的位置
private int x0;
private int y0;
//移动的图形重写绘制
public void rePaintView(){
//移除面板上所有的组件
imagePanel.removeAll();
//重新绘制图片
//遍历二维数组得到图片
for (int i = 0; i < datas.length; i++) {
for (int j = 0; j < datas[i].length; j++) {
JLabel imageLabel=new JLabel(new ImageIcon("Picture-game\\images\\"+datas[i][j]+".png"));
imageLabel.setBounds(j*90,i*90,90,90);
imagePanel.add(imageLabel);
}
}
//把面板添加到窗体上
this.add(imagePanel);
/