1.项目介绍与界面搭建GUI(Graphical User Interface)图形用户接口。采用图像化的方式显示操作界面//JFrame 可以理解为 JaveBean类 JFrame gameJframe new JFrame();2.界面设置//设置界面的宽高this.setSize(603,680); this可以省略不写 //设置界面标题this.setTitle(拼图单机版 v1.0); //设置界面置顶this.setAlwaysOnTop(true); //设置界面居中this.setLocationRelativeTo(null); //设置游戏关闭模式this.setDefaultCloseOperation(3); //设置显示界面窗口(建议写到最后)this.setVisible(true);3.菜单制作//初始化菜单JMenuBar jMenuBar new JMenuBar();JMenu jMenu new JMenu(text); JMenuItem jMenuItem new JMenuItem(text);//初始化菜单 JMenuBar jMenuBar new JMenuBar(); //创建整个菜单对象 功能 关于我们 JMenu functionJMenu new JMenu(功能); JMenu aboutJMenu new JMenu(关于我们); //创建两个对象 下面的条目对象 JMenuItem replayItem new JMenuItem(重新游戏); JMenuItem reLoginItem new JMenuItem(重新登录); JMenuItem closeItem new JMenuItem(关闭游戏); JMenuItem accountItem new JMenuItem(公众号); //将每一个选项下条目添加到相应选项中 functionJMenu.add(replayItem); functionJMenu.add(reLoginItem); functionJMenu.add(closeItem); aboutJMenu.add(accountItem); //将菜单里面的两个选项添加到菜单当中 jMenuBar.add(functionJMenu); jMenuBar.add(aboutJMenu); //给整个界面设置菜单 this.setJMenuBar(jMenuBar);4.添加图片//创建一个图片ImageIcon的对象ImageIconicon new ImageIcon(图片地址);//创建一个JLabel的对象管理容器JLabeljLabel new JLabel(icon);//添加图片指定图片位置setBounds(X,Y,width,height)pxjLabel.setBounds(0,0,105,105);//添加图片去除默认位置this.setLayout(null);//取消默认居中位置只有取消了才会按照XY轴的形式添加组件//窗体窗体.getContentPane().add(JLabel对象);//先获取到里面隐藏容器再添加隐藏容器打乱图片练习打乱一维数组中的数据遇到的问题图片加载数量每次都不一样private void initData() { int[] tempArr {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //2.打乱数组中的数据顺序 //方法遍历数组得到每个元素拿着每一个元素跟随机索引上的数据进行交换 Random r new Random(); for (int i 0; i tempArr.length; i) { //获取随机索引 int index r.nextInt(tempArr.length); int temp tempArr[i]; tempArr[i] tempArr[index]; tempArr[index] temp; //遍历一维数组tempArr得到每一个元素把每一个元素依次添加到二维数组中: for (int i 0; i tempArr.length; i) { data[i / 4][i % 4] tempArr[i]; } } }5.事件KeyListener 键盘监听Override public void keyTyped(KeyEvent e) { //按下能产生可打印字符 触发 } Override public void keyPressed(KeyEvent e) { //按下任意键可以触发 } Override public void keyReleased(KeyEvent e) { //松开触发的事件 }MouseListener 鼠标监听ActionListener 动作监听按钮对象6.美化界面7.移动图片//获取按键值int code e.getKeyCode();//给整个类增加按键监听this.addKeyListener(this);//刷新一下界面this.getContentPane().repaint();//清空已经出现的所有图片this.getContentPane().removeAll();8.查看完整图片作弊码判断胜利if (code 65) { //松开A后还原 //清除所有图片,initImage内已包含 initImage(); //重新加载 } else if (code 87) { //87: w 键一键还原 一件通关 data new int[][]{ {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,0} }; //重新加载图片 initImage(); } } //创建胜利方法 public boolean victory() { //循环一一对比 for (int i 0; i 4; i) { for (int j 0; j 4; j) { if (win[i][j] !data[i][j]) { return false; } } } return true; }9.登录注册界面