import javax.swing.*;
import java.util.Random;
public class GameJFrame extends JFrame {
//JFrame 界面,窗体
//子类呢?也表示界面,窗体
//规定:GameJFrame这个界面表示的就是游戏的主界面
//以后跟游戏相关的所有逻辑都写在这个类中
int date[][] = new int[4][4];
public GameJFrame(){
//设置界面的宽高
initJFrame();
//设置菜单
initJMenuBar();
//初始化数据
initdate();
//设置图片
initimage();
// 让显示显示出来,建议写在最后
this.setVisible(true);
}
private void initdate() {
int[] arr ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for(int i = 0;i<arr.length;i++){
Random r = new Random();
int num = r.nextInt(arr.length);
int temp = arr[i];
arr[i]= arr[num];
arr[num]= temp;
}
int index = 0;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
date[i][j]= arr[index];
index++;
}
}
}
private void initimage() {
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
//创建一个图片ImageIcon的对象
//ImageIcon icon = new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+count+".jpg");
//创建一个Jlabel的对象(管理容器)
JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+date[i][j]+".jpg"));
//指定图片的位置
jLabel.setBounds(105*j,105*i,105,105);
//把管理容器添加到界面中
//this.add(jLabel);
this.getContentPane().add(jLabel);
}
}
}
private void initJMenuBar() {
//创建整个的菜单对象
JMenuBar jMenuBar = new JMenuBar();
//创建菜单上面的两个选项的对象(功能 关于我们)
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
//创建选项下面的条目
JMenuItem repalyItem = new JMenuItem("重新开始");
JMenuItem reloginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公共号");
//将每一个选项下面的条目集中到选项的中
functionJMenu.add(repalyItem);
functionJMenu.add(reloginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
//将选项添加到菜单上
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
//将菜单添加到界面上
this.setJMenuBar(jMenuBar);
}
private void initJFrame() {
this.setSize(603,680);
//设置界面的标题
this.setTitle("拼图单机版 v.10");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置默认的关闭模式
this.setDefaultCloseOperation(3);//0 表示DO_NOTHING_ON_CLOSE 通俗讲:关不掉
//1 表示HIDE_ON_CLOSE 通俗讲:界面关闭但虚拟机未关闭
//2 表示DISPOSE_ON_CLOSE 通俗讲:当开启多个界面的时候,只有把所有界面都关闭虚拟机才会停止。注意:所有界面都要设置成这个样子。
//3 表示EXIT_ON_CLOSE 通俗讲: 关掉一个界面,虚拟机就停止了。
//取消居中放置,按照xy轴的形式添加组件
this.setLayout(null);
}
}
JAVA 拼图小游戏(打乱图片)
最新推荐文章于 2024-10-06 20:16:20 发布