欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > JAVA题目笔记(十二) 拼图游戏

JAVA题目笔记(十二) 拼图游戏

2025/6/25 17:57:07 来源:https://blog.csdn.net/DDDiccc/article/details/143382475  浏览:    关键词:JAVA题目笔记(十二) 拼图游戏

一、创建界面:

创建一个603×680像素大小的游戏主界面。

创建一个488×430像素的登录界面

创建一个488×500像素的注册界面

方法代码示例:

        JFrame gameJFrame=new JFrame();  //创建界面对象gameJFrame.setSize(603,680);  //设置界面大小   界面默认隐藏gameJFrame.setVisible(true);   //显示/隐藏界面    方法参数为boolean变量,true则显示界面,false隐藏。

二、编辑界面

设置界面标题及各种内容。

相关方法示例:

        JFrame jframe=new JFrame();  //创建界面对象 jframe.setTitle("游戏标题");  //设置界面标题jframe.setAlwaysOnTop(true); //界面置顶jframe.setLocationRelativeTo(null);  //设置界面初始位置居中jframe.setDefaultCloseOperation(3);  //设置关闭模式  参数为int //0:点关闭啥也不做   1:默认关闭//2:若有多个界面则只有在关闭最后一个界面时虚拟机才会终止,前提:每个界面关闭模式都为2//3:只要关闭其中一个界面,虚拟机直接停止

三、编辑界面菜单

设置界面中菜单和菜单中的不同功能以及对应的各种条目

相关方法示例:

JFrame jframe=new JFrame();  //创建界面对象//创建 菜单 对象JMenuBar jmenuBar=new JMenuBar();//创建 菜单选项1 和 菜单选项2 对象JMenu functionJMenu=new JMenu("功能");JMenu aboutJMenu=new JMenu("关于我们");//创建 条目 对象JMenuItem relodGame=new JMenuItem("重新开始");JMenuItem relogIn=new JMenuItem("重新登录");JMenuItem closeGame=new JMenuItem("关闭游戏");//创建 条目 对象JMenuItem officialAccount=new JMenuItem("公众号");//调用方法将条目对象添加到菜单选项1中functionJMenu.add(relogIn);functionJMenu.add(relodGame);functionJMenu.add(closeGame);//调用方法将条目对象添加到菜单选项2中aboutJMenu.add(officialAccount);//将选项添加到菜单中jmenuBar.add(functionJMenu);jmenuBar.add(aboutJMenu);//将菜单添加到界面中jframe.setJMenuBar(jmenuBar);

四、添加图片、调整图片位置

添加图片相关方法示例代码:

//创建图片ImageIcon对象 
ImageIcon icon=new ImageIcon("C:\\Users\\DDDic\\IdeaProjects\\PuzzleGame\\image\\animal\\animal3\\3.jpg");
//构造方法参数为图片文件地址//创建管理容器对象   JLabel 将图片对象icon放入
JLabel jLabel=new Jlabel(icon);//将容器添加进界面对象jmenuBar中
jmenuBar.add(jLabel);//指定图片位置创建图片和管理容器对象代码合并:
JLabel jLabel=new Jlabel(new ImageIcon("C:\\Users\\DDDic\\IdeaProjects\\PuzzleGame\\image\\animal\\animal3\\3.jpg"));

调整图片位置相关代码:

//设置图片位置
//方法参数为:(x坐标,y坐标,图片宽度,图片长度)
jLabel.setBounds(105*j,105*i,105,105); //将图片添加入界面中
GameJFrame.getContentPane().add(jLabel);//getContentPane 方法就是调用下图中红色框表示的容器,将图片加入该容器

调整位置需要在界面初始化代码中增加以下代码:

        this.setAlwaysOnTop(true);  //true 置顶窗口this.setLocationRelativeTo(null);  //设置界面初始位置居中//取消界面默认的居中放置,将其设置为null,按照x,y轴坐标放置图片GameJFrame.setLayout(null);  

原因:图中红色框内是创建界面时自动生成的一个容器,默认状态会将接受的图片或者其他类进行居中放置,因此需要调用方法取消默认的居中放置。

五、添加事件

事件源:按钮/图片/窗体

事件:某些操作  如:鼠标单击  鼠标划入....

监听绑定:当事件源上发生了某个事件,则执行某段代码。

键盘监听:KeyListener      鼠标监听:MouseListener   动作监听:ActionListener

给按钮添加动作事件:

示例代码:

package com.java.ui;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("按钮按下了");}}package com.java.ui;import javax.swing.*;
import java.util.Random;public class Test {public static void main(String[] args){JFrame jframe=new JFrame();  //创建界面对象jframe.setSize(500,500);jframe.setLayout(null);//创建一个按钮对象JButton jtb=new JButton("点击按钮");//设置坐标位置jtb.setBounds(0,0,100,50);//给按钮添加动作监听  方法参数为ActionListener 接口类型,因此需要使用这个接口的实现类 表示触发后执行的代码jtb.addActionListener(new MyActionListener());//将按钮加入界面中jframe.getContentPane().add(jtb);jframe.setVisible(true);}
}

由于实际情况中,每个按钮功能不一样,且动作监听接口的实现类只会在触发时被调用一次,因此可以采用 匿名内部类的形式进行简化代码:

package com.java.ui;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class Test {public static void main(String[] args){JFrame jframe=new JFrame();  //创建界面对象jframe.setSize(500,500);jframe.setLayout(null);//创建一个按钮对象JButton jtb=new JButton("点击按钮");//设置坐标位置jtb.setBounds(0,0,100,50);//给按钮添加动作监听  方法参数为ActionListener 接口类型,//因此需要使用这个接口的实现类 表示触发后执行的代码jtb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("按钮被按咯");}});//将按钮加入界面中jframe.getContentPane().add(jtb);jframe.setDefaultCloseOperation(3);jframe.setVisible(true);}
}

除此以外还有另一种方式:

package com.java.ui;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class MyJFrame extends JFrame implements ActionListener {//创建一个按钮对象JButton jtb=new JButton("点击按钮1");//创建另一个按钮对象JButton jtb2=new JButton("点击按钮2");public MyJFrame(){this.setSize(500,500);this.setLayout(null);//设置坐标位置jtb.setBounds(0,0,100,50);jtb2.setBounds(100,0,100,50);//给按钮添加动作监听  方法参数为ActionListener 接口类型,因此需要使用这个接口的实现类 来执行实现类中设定的触发后的执行代码//而本类已经是ActionListener接口的实现类,因此this记录的就是本类地址,直接进入本类中对应重写执行代码部分,执行触发代码。jtb.addActionListener(this);jtb2.addActionListener(this);//将按钮加入界面中this.getContentPane().add(jtb);this.getContentPane().add(jtb2);this.setDefaultCloseOperation(3);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {//对当前按钮进行判断Random r=new Random();Object source=e.getSource();//getSource()方法:获取当前被操作的按钮对象if(source==jtb)jtb.setSize(200,200);//设置按钮大小else if(source==jtb2)jtb2.setBounds(r.nextInt(500),r.nextInt(500),100,50);}
}
package com.java.ui;public class Test {public static void main(String[] args){new MyJFrame();}
}

给按钮添加鼠标事件:

package com.java.ui;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;public class MyJFrame extends JFrame implements MouseListener {public MyJFrame(){this.setSize(500,500);//创建一个按钮对象JButton jtb=new JButton("点击按钮1");this.setLayout(null);//设置按钮坐标位置jtb.setBounds(0,0,100,50);//给按钮绑定鼠标监听  本类就是MouseListener接口的实现类,因此直接用this代表实现类jtb.addMouseListener(this);this.getContentPane().add(jtb);this.setVisible(true);}@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("单击");}@Overridepublic void mousePressed(MouseEvent e) {System.out.println("按下不松");}@Overridepublic void mouseReleased(MouseEvent e) {System.out.println("松开");}@Overridepublic void mouseEntered(MouseEvent e) {System.out.println("划入");}@Overridepublic void mouseExited(MouseEvent e) {System.out.println("划出");}
}package com.java.ui;public class Test {public static void main(String[] args){new MyJFrame();}
}

给窗体添加键盘事件:

package com.java.ui;import javax.swing.*;
import java.awt.event.*;
import java.util.Random;public class MyJFrame extends JFrame implements KeyListener {public MyJFrame(){this.setSize(500,500);this.setLayout(null);//给窗口增加键盘监听this.addKeyListener(this);this.setVisible(true);}@Overridepublic void keyTyped(KeyEvent e) {}//若按下不松,则会重复调用KeyPressed方法@Overridepublic void keyPressed(KeyEvent e) {System.out.println("按下");}//区分键盘按下的按键是哪一个 使用  getKeyCode方法//返回的是每个按键的编号@Overridepublic void keyReleased(KeyEvent e) {System.out.println("松开");int code=e.getKeyCode();System.out.println((char)code);}
}

六、为拼图添加背景图片、为图片添加边框

Tips:

在窗口中添加图片,先添加的图片在上方,后添加的图片在下方,因此背景图片需要放在最后添加进窗口。

添加边框代码:

        //先加载的图片在上方,后加载的图片在下面,背景图片放最后。ImageIcon icon=new ImageIcon("C:\\Users\\DDDic\\IdeaProjects\\PuzzleGame\\image\\background.png");JLabel jbar=new JLabel(icon);//添加边框是对容器类JLabel 的对象调用setBorder()方法//BevelBorder()是边框类  参数0表示凸起   1表示凹陷// 0也可以用 BevelBorder.RAISED 表示   // 1也可以用BevelBorder.LOWERED表示jbar.setBorder(new BevelBorder(0));  jbar.setBounds(40,40,508,560);this.getContentPane().add(jbar);

七、代码路径调整

路径种类:

①绝对路径:从盘符开始 :C:\     D:\

②相对路径:不是从盘符开始 : aaa\bbb     在当前项目下去找aaa文件夹,里面去找bbb文件夹

路径转换:

//绝对路径
JLabel jLabel=new JLabel(new ImageIcon("C:\\Users\\DDDic\\IdeaProjects\\PuzzleGame\\image\\animal\\animal3\\"+number+".jpg"));//相对路径  从模块名字开始image开始(大家都在一个项目里,没有相对的意义,删去项目名)JLabel jLabel=new JLabel(new ImageIcon("image\\animal\\animal3\\"+number+".jpg"));

八、拼图移动

通过给界面添加键盘监听来实现:

    public void keyReleased(KeyEvent e) {//对上下左右进行判断//左37  上38 右39 下40int code=e.getKeyCode();if(code==37) {System.out.println("向左移动");}else if(code==38){System.out.println("向上移动");}else if(code==39){System.out.println("向右移动");}else if(code==40){System.out.println("向下移动");}}

每次移动都会重新生成移动后的图片,为了避免后续生成的图片被遮盖,每次生成新图片都要清空已经生成的图片+刷新界面:

        jframe.getContentPane().removeAll();    //清除所有界面中图片//新的图片代码jframe.getContentPane().repaint();   //刷新界面内容

九、作弊、查看原图、胜利判定

作弊:

将图片正确排列的次序定义为一个静态数组,在键盘监听函数中设置一个键值,当读取到键值时,直接将当前排列次序数组变为该数组。

    int[][] data=new int[4][4];int[][] win=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};public void keyReleased(KeyEvent e) { if (code == 87) {  // 作弊键值设置为 WFinialImage();} }//注意不能直接将win数组赋值给data,数组为引用数据类型,直接赋值是将地址值赋予daata
//会影响后续胜利的判断private void FinialImage() {data=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};x=3;y=3;InitialImage();}

不能直接将win数组赋值给data,数组为引用数据类型,直接赋值是将地址值赋予data,会影响后续胜利的判断。


查看原图:

原理同上

public void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65) {  //将键值设定为  Athis.getContentPane().removeAll();    //清除所有界面中图片//加载完整图片JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));jLabel.setBorder(new BevelBorder(1));jLabel.setBounds(83, 134, 420, 420);this.getContentPane().add(jLabel);//加载背景图片ImageIcon icon = new ImageIcon("image\\background.png");JLabel background = new JLabel(icon);background.setBorder(new BevelBorder(1));background.setBounds(40, 40, 508, 560);this.getContentPane().add(background);//刷新界面内容this.getContentPane().repaint();}}public void keyReleased(KeyEvent e) {if (code == 65)  //A键松开后重新恢复原状InitialImage();}

 胜利判定:

定义一个函数,将数组data 与 win 依次对比,返回boolean变量,前文键盘监听操作都是基于 未胜利 进行,一旦胜利则不再进行对应操作。

    public boolean victory(){for(int i=0;i<4;i++){for(int j=0;j<4;j++){if(data[i][j]!=win[i][j])return false;}}return true;}

十、增加菜单条目功能

为条目绑定动作监听,重写动作监听接口中方法:

JMenuItem officialAccount=new JMenuItem("公众号");
officialAccount.addActionListener(this);
   public void actionPerformed(ActionEvent e) {//通过 getSource方法来判断 动作监听接收到的信号是哪一个//因此条目对象的创建需要写在成员方法中Object o=e.getSource();if (o == relodGame) {System.out.println("重新开始");} else if (o == relogIn) {System.out.println("重新登录");} else if (o == aboutJMenu) {System.out.println("关于我们");} else if (o == closeGame) {System.out.println("关闭游戏");} else if (o == officialAccount) {//弹出账号二维码}}

动作监听(多种设计):

弹窗:

//创建弹窗对象  JDialogJDialog jDialog=new JDialog();JLabel jLabel=new JLabel(new ImageIcon("image//damie.jpg"));jLabel.setBounds(0,0,258,258);//弹框和界面JFrame一样有隐藏容器,需要调用getContenPane方法再将图片容器添加。jDialog.getContentPane().add(jLabel);//设置弹框大小jDialog.setSize(344,344);//弹框置顶jDialog.setAlwaysOnTop(true);//弹框居中jDialog.setLocationRelativeTo(null);//不关闭弹框则无法进行下一步操作jDialog.setModal(true);//显示弹框jDialog.setVisible(true);

关闭游戏:

//直接关闭虚拟机
System.exit(0);

重新游戏:

           System.out.println("重新开始");//刷新步数stepcount=0;//初始化数据(将图片顺序打乱)InitialData();//初始化图片InitialImage();

重新登陆:

            System.out.println("重新登录");//隐藏当前游戏界面this.setVisible(false);//打开登录界面new LoginJFrame();

十一、密码框对象

明文密码框:

JTextField accountField=new JTextField();

密文密码框:

JPasswordField passwordField=new JPasswordField();//设置大小
passwordField.setBounds(195,195,200,30);
//添加进窗口容器
JFrame.getContentPane().add(passwordField);//获取框内字符串
passwordField.getText();//设置框内字符串内容
passwordField.setText("字符串内容");

 

十二、完整代码(注册部分涉及到数据库,暂时先放一放)

工具类:

package com.java.ui;import java.util.Random;public class CodeUtil {private CodeUtil(){}public static String getcode(){Random r = new Random();char code[] = new char[5];int index = r.nextInt(5);char number = (char) (r.nextInt(10) + 48);for (int i = 0; i < code.length; i++) {if (i != index) {code[i] = (char) (r.nextInt(26) + 65);} elsecode[i] = number;}String result = new String(code);return result;}
}

游戏窗口类+注册窗口类+登录窗口类+用户类:

游戏窗口类:

package com.java.ui;import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;public class GameJFrame extends JFrame implements KeyListener, MouseListener, ActionListener {//菜单选项JMenu functionJMenu=new JMenu("功能");JMenu aboutJMenu=new JMenu("关于我们");//选项条目//二级菜单  "更换图片"JMenu changeImage=new JMenu("更换图片");//更换图片的选项(二级菜单的条目)JMenuItem animal=new JMenuItem("动物");JMenuItem girls=new JMenuItem("美女");JMenuItem sports=new JMenuItem("运动");JMenuItem relodGame=new JMenuItem("重新开始");JMenuItem relogIn=new JMenuItem("重新登录");JMenuItem closeGame=new JMenuItem("关闭游戏");JMenuItem officialAccount=new JMenuItem("公众号");int[][] data=new int[4][4];int[][] win=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};int stepcount=0;int x,y; //记录空白方块在二维数组中的位置//图片分类String[] animalArray=new String[]{"image\\animal\\animal1\\","image\\animal\\animal2\\","image\\animal\\animal3\\","image\\animal\\animal4\\","image\\animal\\animal5\\","image\\animal\\animal6\\","image\\animal\\animal7\\","image\\animal\\animal8\\"};String[] girlArray=new String[]{"image\\girl\\girl1\\","image\\girl\\girl2\\","image\\girl\\girl3\\","image\\girl\\girl4\\","image\\girl\\girl5\\","image\\girl\\girl6\\","image\\girl\\girl7\\","image\\girl\\girl8\\","image\\girl\\girl9\\","image\\girl\\girl10\\","image\\girl\\girl11\\","image\\girl\\girl12\\","image\\girl\\girl13\\"};String[] sportArray=new String[]{"image\\sport\\sport1\\","image\\sport\\sport2\\","image\\sport\\sport3\\","image\\sport\\sport4\\","image\\sport\\sport5\\","image\\sport\\sport6\\","image\\sport\\sport7\\","image\\sport\\sport8\\","image\\sport\\sport9\\","image\\sport\\sport10\\"};String path="image\\girl\\girl3\\";public GameJFrame(){//界面初始化InitialJFrame();//菜单初始化InitialMenu();//初始化数据(将图片顺序打乱)InitialData();//初始化图片InitialImage();//    InitialButton();//显示/隐藏界面    方法参数为boolean变量,true则显示界面,false隐藏。this.setVisible(true);}private void InitialButton() {for(int i=0;i<4;i++){for(int j=0;j<4;j++){JButton jbt = new JButton(" "+data[i][j]);jbt.setBounds(105*j+83,105*i+134,105,105);jbt.setName(" "+data[i][j]);jbt.addMouseListener(this);this.getContentPane().add(jbt);}}}private void InitialData() {int [] tempArr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};Random r=new Random();for(int i=0;i<16;i++){int index=r.nextInt(16-i)+i;int temp=tempArr[i];tempArr[i]=tempArr[index];tempArr[index]=temp;}int count=0;for(int i=0;i<4;i++){for(int j=0;j<4;j++){data[i][j]=tempArr[count];if(data[i][j]==0){x=i;y=j;}count++;}}}private void InitialImage() {//初始化图片代码//清空已经生成的图片(先生成的图片在前面,会导致后续操作生成的图片被遮盖)this.getContentPane().removeAll();if(victory()){System.out.println("win!");JLabel jlabel=new JLabel(new ImageIcon("image//win.png"));jlabel.setBounds(203,283,197,73);this.getContentPane().add(jlabel);}else{this.getContentPane().removeAll();}JLabel str=new JLabel("步数:"+stepcount);str.setBounds(10,0,60,60);this.getContentPane().add(str);for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {int number = data[i][j];//创建一个图片ImageIcon的对象  (图片需要放入到JLabel类的容器中,才能添加进界面)JLabel jLabel = new JLabel(new ImageIcon(path + number + ".jpg"));jLabel.setBorder(new BevelBorder(1));jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);this.getContentPane().add(jLabel);//number==16时,找不到名为16的图片路径,创建的JLabel对象为空白,将其添加到对应位置。}}//先加载的图片在上方,后加载的图片在下面,背景图片放最后。ImageIcon icon = new ImageIcon("image\\background.png");JLabel background = new JLabel(icon);background.setBorder(new BevelBorder(1));background.setBounds(40, 40, 508, 560);this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}private void InitialMenu() {//菜单初始化代码//菜单JMenuBar jmenuBar=new JMenuBar();//菜单选项functionJMenu.addActionListener(this);aboutJMenu.addActionListener(this);//选项1条目changeImage.addActionListener(this);relodGame.addActionListener(this);relogIn.addActionListener(this);closeGame.addActionListener(this);//选项2条目officialAccount.addActionListener(this);//二级菜单条目girls.addActionListener(this);animal.addActionListener(this);sports.addActionListener(this);//将二级菜单条目添加到二级菜单中changeImage.add(girls);changeImage.add(animal);changeImage.add(sports);//将选项1包含的条目添加到选项中functionJMenu.add(changeImage);functionJMenu.add(relogIn);functionJMenu.add(relodGame);functionJMenu.add(closeGame);//将选项2包含的条目添加到选项中aboutJMenu.add(officialAccount);//将选项添加到菜单中jmenuBar.add(functionJMenu);jmenuBar.add(aboutJMenu);//将菜单添加到界面中this.setJMenuBar(jmenuBar);}private void InitialJFrame() {  //游戏主界面代码//创建游戏主界面this.setSize(603,680);  //设置界面大小   界面默认隐藏//设置标题this.setTitle("拼图游戏");//设置置顶this.setAlwaysOnTop(true);  //true 置顶窗口this.setLocationRelativeTo(null);  //设置界面初始位置居中this.setDefaultCloseOperation(3);//设置关闭模式  参数为int 类型 0:点×啥也不做  1: 默认关闭//2:若有多个界面则只有在关闭最后一个界面时虚拟机才会终止,前提:每个界面关闭模式都为2// 3:只要关闭其中一个界面,虚拟机直接停止//取消默认的居中放置,按照x,y轴的方式放置图片this.setLayout(null);this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {if(!victory()) {int code = e.getKeyCode();if (code == 65) {this.getContentPane().removeAll();    //清除所有界面中图片//加载完整图片JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));jLabel.setBorder(new BevelBorder(1));jLabel.setBounds(83, 134, 420, 420);this.getContentPane().add(jLabel);//加载背景图片ImageIcon icon = new ImageIcon("image\\background.png");JLabel background = new JLabel(icon);background.setBorder(new BevelBorder(1));background.setBounds(40, 40, 508, 560);this.getContentPane().add(background);//刷新界面内容this.getContentPane().repaint();}}else return;}@Overridepublic void keyReleased(KeyEvent e) {if(!victory()){InitialImage();//对上下左右进行判断//左37  上38 右39 下40int code = e.getKeyCode();if (code == 37) {if (y < 3) {System.out.println("向左移动");//此时空白方块坐标: x,y  右边需要左移的方块坐标 x ,y+1int temp = data[x][y];data[x][y] = data[x][y + 1];data[x][y + 1] = temp;y = y + 1;stepcount++;InitialImage();} else {}} else if (code == 38) {if (x < 3) {System.out.println("向上移动");//此时空白方块坐标: x,y  下边需要上移的方块坐标 x+1 ,yint temp = data[x][y];data[x][y] = data[x + 1][y];data[x + 1][y] = temp;x = x + 1;stepcount++;InitialImage();} else {}} else if (code == 39) {if (y > 0) {System.out.println("向右移动");//此时空白方块坐标: x,y  左边需要右移的方块坐标 x ,y-1int temp = data[x][y];data[x][y] = data[x][y - 1];data[x][y - 1] = temp;y = y - 1;stepcount++;InitialImage();} else {}} else if (code == 40) {if (x > 0) {System.out.println("向下移动");//此时空白方块坐标: x,y  上面需要下移的方块坐标 x-1 ,yint temp = data[x][y];data[x][y] = data[x - 1][y];data[x - 1][y] = temp;x = x - 1;stepcount++;InitialImage();}} else if (code == 87) {FinialImage();} else if (code == 65) {InitialImage();}}else return;}private void FinialImage() {stepcount++;data=new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};x=3;y=3;InitialImage();}public boolean victory(){for(int i=0;i<4;i++){for(int j=0;j<4;j++){if(data[i][j]!=win[i][j])return false;}}return true;}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}@Overridepublic void actionPerformed(ActionEvent e) {Random r=new Random();Object o=e.getSource();if (o == relodGame) {System.out.println("重新开始");//初始化数据(将图片顺序打乱)stepcount=0;InitialData();//初始化图片InitialImage();} else if (o == relogIn) {System.out.println("重新登录");this.setVisible(false);new LoginJFrame();//} else if (o == aboutJMenu) {System.out.println("关于我们");//} else if (o == closeGame) {System.out.println("关闭游戏");//关闭虚拟机System.exit(0);//} else if (o == officialAccount) {//创建弹窗对象  JDialogJDialog jDialog=new JDialog();JLabel jLabel=new JLabel(new ImageIcon("image//damie.jpg"));jLabel.setBounds(0,0,258,258);//弹框和界面JFrame一样有隐藏容器,需要调用getContenPane方法再将图片容器添加。jDialog.getContentPane().add(jLabel);//设置弹框大小jDialog.setSize(344,344);//弹框置顶jDialog.setAlwaysOnTop(true);//弹框居中jDialog.setLocationRelativeTo(null);//不关闭弹框则无法进行下一步操作jDialog.setModal(true);//显示弹框jDialog.setVisible(true);}else if(o==girls){//String path="image\\girl\\girl3\\";int index=r.nextInt(13)+1;path=girlArray[index];//重置步数stepcount=0;//打乱图片InitialData();//初始化图片InitialImage();}else if(o==sports){int index=r.nextInt(10)+1;path=sportArray[index];//重置步数stepcount=0;//打乱图片InitialData();//初始化图片InitialImage();}else if(o==animal){int index=r.nextInt(8)+1;path=animalArray[index];//重置步数stepcount=0;//打乱图片InitialData();//初始化图片InitialImage();}}
}

 注册窗口类:

package com.java.ui;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Scanner;public class RegisterJFrame extends JFrame implements ActionListener, MouseListener {Scanner sc=new Scanner(System.in);//账号输入框(明文输入框)JTextField accountField=new JTextField(1);//注册密码输入框(明文)JTextField passwordField=new JTextField(1);//再次输入密码框(明文)JTextField rePasswordField=new JTextField(1);//注册+重置按钮JButton registerButton=new JButton();JButton initialButton=new JButton();public RegisterJFrame(){InitialRegisterJFrame();InitialImage();this.setVisible(true);}private void InitialRegisterJFrame() {//创建注册界面this.setSize(488,430);this.setTitle("注册");this.setAlwaysOnTop(true);  //true 置顶窗口this.setLocationRelativeTo(null);  //设置界面初始位置居中this.setDefaultCloseOperation(3);this.setLayout(null);//取消内部默认布局}private void InitialImage(){//去除容器内所有图片this.getContentPane().removeAll();//注册密码图片+注册账号图片JLabel registerAccount=new JLabel(new ImageIcon("image\\register\\注册用户名.png"));registerAccount.setBounds(110,135,79,17);this.getContentPane().add(registerAccount);JLabel registerPassword=new JLabel(new ImageIcon("image\\register\\注册密码.png"));registerPassword.setBounds(125,195,64,16);this.getContentPane().add(registerPassword);JLabel registerRePassword=new JLabel(new ImageIcon("image\\register\\再次输入密码.png"));registerRePassword.setBounds(95,255,96,17);this.getContentPane().add(registerRePassword);//输入框accountField.setBounds(195,134,200,30);this.getContentPane().add(accountField);passwordField.setBounds(195,195,200,30);this.getContentPane().add(passwordField);rePasswordField.setBounds(195,255,200,30);this.getContentPane().add(rePasswordField);//注册按钮 + 重置按钮registerButton.setIcon(new ImageIcon("image\\register\\注册按钮.png"));registerButton.setBounds(123,310,128,47);initialButton.setIcon(new ImageIcon("image\\register\\重置按钮.png"));initialButton.setBounds(256,310,128,47);//删除按钮边框registerButton.setBorderPainted(false);initialButton.setBorderPainted(false);//删除按钮背景registerButton.setContentAreaFilled(false);initialButton.setContentAreaFilled(false);//绑定动作监听registerButton.addMouseListener(this);initialButton.addMouseListener(this);this.getContentPane().add(registerButton);this.getContentPane().add(initialButton);//背景图片JLabel jLabel=new JLabel(new ImageIcon("image\\register\\background.png"));jLabel.setBounds(0,0,470,390);this.getContentPane().add(jLabel);this.getContentPane().repaint();}@Overridepublic void actionPerformed(ActionEvent e) {}@Overridepublic void mouseClicked(MouseEvent e) {Object o=e.getSource();if(o==registerButton){showJDialog("注册成功!");this.setVisible(false);new LoginJFrame();}else if(o==initialButton){passwordField.setText("");rePasswordField.setText("");accountField.setText("");}}public void showJDialog(String content){JDialog jDialog=new JDialog();JLabel jLabel=new JLabel(content); //创建 字符串 的JLabel容器对象jLabel.setBounds(0,0,200,150);jDialog.add(jLabel);jDialog.setSize(200,150);jDialog.setAlwaysOnTop(true);  //置顶jDialog.setLocationRelativeTo(null);  //居中jDialog.setModal(true); //设置不关闭则无法进行其他操作jDialog.setVisible(true);}@Overridepublic void mousePressed(MouseEvent e) {Object o=e.getSource();if(o==registerButton){registerButton.setIcon(new ImageIcon("image\\register\\注册按下.png"));}else if(o==initialButton){initialButton.setIcon(new ImageIcon("image\\register\\重置按下.png"));}}@Overridepublic void mouseReleased(MouseEvent e) {Object o=e.getSource();if(o==registerButton){registerButton.setIcon(new ImageIcon("image\\register\\注册按钮.png"));}else if(o==initialButton){initialButton.setIcon(new ImageIcon("image\\register\\重置按钮.png"));}}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}
}

登录窗口类:

package com.java.ui;import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;public class LoginJFrame extends JFrame implements MouseListener, KeyListener , ActionListener {static ArrayList<User> list=new ArrayList<>();static {list.add(new User("jinluhao121262", "121262"));list.add(new User("zhangsiting5257", "123012"));}//成员变量处定义,方便监听判断JButton logIn=new JButton();JButton register=new JButton();//账号输入框(明文输入框类  JTextField)    (密文输入框类  JPasswordField)JTextField accountField=new JTextField(1);JPasswordField passwordField=new JPasswordField(1);//获取验证码String code=CodeUtil.getcode();//验证码输入框JTextField verifycodefield=new JTextField(1);//验证码显示区JLabel rightCode=new JLabel(code);public LoginJFrame(){Scanner sc=new Scanner(System.in);initialJFrame();initialImage();this.setVisible(true);}private void initialImage() {this.getContentPane().removeAll();accountField.setBounds(195,134,200,30);this.getContentPane().add(accountField);//密码输入框passwordField.setBounds(195,195,200,30);passwordField.addActionListener(this);this.getContentPane().add(passwordField);verifycodefield.setBounds(195,256,50,30);this.getContentPane().add(verifycodefield);//添加验证码显示区rightCode.setBounds(300,256,50,30);rightCode.addMouseListener(this);this.getContentPane().add(rightCode);//添加登录  注册按钮 +  按钮绑定鼠标监听logIn.setIcon(new ImageIcon("image\\login\\登录按钮.png"));logIn.setBounds(123,310,128,47);logIn.addMouseListener(this);register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));register.setBounds(256,310,128,47);register.addMouseListener(this);//去除按钮边框logIn.setBorderPainted(false);register.setBorderPainted(false);//去除按钮背景logIn.setContentAreaFilled(false);register.setContentAreaFilled(false);//显示this.getContentPane().add(logIn);this.getContentPane().add(register);JLabel account=new JLabel(new ImageIcon("image\\login\\用户名.png"));account.setBounds(116,135,47,17);this.getContentPane().add(account);JLabel password=new JLabel(new ImageIcon("image\\login\\密码.png"));password.setBounds(130,195,32,16);this.getContentPane().add(password);JLabel verifycode=new JLabel(new ImageIcon("image\\login\\验证码.png"));verifycode.setBounds(110,256,56,21);this.getContentPane().add(verifycode);//添加背景图片//将背景图片加入容器JLabel jLabel=new JLabel(new ImageIcon("image\\login\\background.png"));jLabel.setBounds(0,0,470,390);//调用隐藏容器,再加入图片容器this.getContentPane().add(jLabel);this.getContentPane().repaint();}private void initialJFrame() {this.setSize(488,430);this.setTitle("登录");this.setAlwaysOnTop(true);  //true 置顶窗口this.setLocationRelativeTo(null);  //设置界面初始位置居中this.setDefaultCloseOperation(3);this.setLayout(null);//取消内部默认布局}public void checkAccountPassword(){if(verifycodefield.getText().equalsIgnoreCase(code)){if (accountField.getText().length() == 0 || passwordField.getPassword().length == 0) {showJDialog("用户名或密码不能为空!");} else if (!accountField.getText().equals(list.get(0).getAccount()) && accountField.getText().equals(list.get(1).getAccount())) {showJDialog("用户名不存在!");} else if (accountField.getText().equals(list.get(0).getAccount())) {if (new String(passwordField.getPassword()).equals(list.get(0).getPassword())) {showJDialog("登陆成功");this.setVisible(false);new GameJFrame();} else {showJDialog("密码错误!");}} else if (accountField.getText().equals(list.get(1).getAccount())) {if (new String(passwordField.getPassword()).equals(list.get(1).getPassword())) {showJDialog("登陆成功");} else {showJDialog("密码错误!");}}}else showJDialog("验证码错误!");}public void showJDialog(String content){JDialog jDialog=new JDialog();JLabel jLabel=new JLabel(content); //创建 字符串 的JLabel容器对象jLabel.setBounds(0,0,200,150);jDialog.add(jLabel);jDialog.setSize(200,150);jDialog.setAlwaysOnTop(true);  //置顶jDialog.setLocationRelativeTo(null);  //居中jDialog.setModal(true); //设置不关闭则无法进行其他操作jDialog.setVisible(true);}@Overridepublic void mouseClicked(MouseEvent e) {Object o=e.getSource();if(o==logIn){checkAccountPassword();}else if(o==rightCode){code=CodeUtil.getcode();rightCode=new JLabel(code);initialImage();}else if(o==register){this.setVisible(false);new RegisterJFrame();}}@Overridepublic void mousePressed(MouseEvent e) {Object o=e.getSource();if(o==logIn){logIn.setIcon(new ImageIcon("image\\login\\登录按下.png"));}else if(o==register){register.setIcon(new ImageIcon("image\\login\\注册按下.png"));}}@Overridepublic void mouseReleased(MouseEvent e) {Object o=e.getSource();if(o==logIn){logIn.setIcon(new ImageIcon("image\\login\\登录按钮.png"));}else if(o==register){register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));}}@Overridepublic void mouseEntered(MouseEvent e) {Object o=e.getSource();if(o==logIn){}}@Overridepublic void mouseExited(MouseEvent e) {Object o=e.getSource();if(o==logIn){}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void actionPerformed(ActionEvent e) {}
}

用户类: 

package com.java.ui;public class User {private String account;private String password;public User(){}public User(String account,String password){this.account=account;this.password=password;}/*** 获取* @return account*/public String getAccount() {return account;}/*** 设置* @param account*/public void setAccount(String account) {this.account = account;}/*** 获取* @return password*/public String getPassword() {return password;}/*** 设置* @param password*/public void setPassword(String password) {this.password = password;}public String toString() {return "User{account = " + account + ", password = " + password + "}";}
}

主函数:

import com.java.ui.GameJFrame;
import com.java.ui.LoginJFrame;
import com.java.ui.RegisterJFrame;import javax.swing.*;
import java.util.ArrayList;public class Main {public static void main(String[] args) {//主程序启动入口new LoginJFrame();}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词