Hello,各位大家好。今天,我们要写一个非常有意思的小游戏 —《拼图小游戏》
我们先来看一下游戏的最终成品展示,然后再一步一步的从0开始,完成游戏里面每一个细节。
游戏运行之后,就是这样的界面。

刚开始打开,是登录页面,因为是第一次运行,需要注册。点击注册就会跳转到注册页面

在注册页面我们可以注册账号,用户名如果已存在则会注册失败。

在游戏主界面中,我们可以利用上下左右移动小图片去玩游戏,还有快捷键A可以查看最终效果,W一键通关。
我们在写游戏的时候,也是一部分一部分完成的。
先写游戏主界面,实现步骤如下:
1,完成最外层窗体的搭建。
2,再把菜单添加到窗体当中。
3,把小图片添加到窗体当中。
4,打乱数字图片的顺序。
5,让数字图片可以移动起来。
6,通关之后的胜利判断。
7,添加其他额外的功能。

创建App类, 编写main方法
作用: 程序的主入口
书写以下代码
//1.召唤主界面
JFrame jFrame = new JFrame();//2.设置主界面的大小
jFrame.setSize(514,595);//3.让主界面显示出来
jFrame.setVisible(true);
//1.召唤主界面
JFrame jFrame = new JFrame();//设置主界面的大小
jFrame.setSize(514,595);//将主界面设置到屏幕的正中央
jFrame.setLocationRelativeTo(null);//将主界面置顶
jFrame.setAlwaysOnTop(true);//关闭主界面的时候让代码一起停止
jFrame.setDefaultCloseOperation(3);//给主界面设置一个标题
jFrame.setTitle("拼图游戏单机版 v1.0");//2.让主界面显示出来
jFrame.setVisible(true);
注意事项:
jFrame.setVisible(true);必须要写在最后一行。
需求:
如果把所有的代码都写在main方法中,那么main方法里面的代码,就包含游戏主界面的代码,登录界面的代码,注册界面的代码,会变得非常臃肿后期维护也是一件非常难的事情,所以我们需要用继承改进,改进之后,代码就可以分类了。
//登录界面
public class LoginJFrame extends JFrame {//LoginJFrame 表示登录界面//以后所有跟登录相关的代码,都写在这里public LoginJFrame(){//在创建登录界面的时候,同时给这个界面去设置一些信息//比如,宽高,直接展示出来this.setSize(488,430);//设置界面的标题this.setTitle("拼图 登录");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//让显示显示出来,建议写在最后this.setVisible(true);}
}//注册界面
public class RegisterJFrame extends JFrame {//跟注册相关的代码,都写在这个界面中public RegisterJFrame(){this.setSize(488,500);//设置界面的标题this.setTitle("拼图 注册");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//让显示显示出来,建议写在最后this.setVisible(true);getContentPane();}
}//游戏主界面
public class GameJFrame extends JFrame {public GameJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//让界面显示出来,建议写在最后this.setVisible(true);}
}
注意点:
其中this表示当前窗体的意思。
菜单就是下图红色边框的内容。


在菜单中有:JMenuBar、JMenu、JMenuItem三个角色。
JMenuBar:如上图中红色边框
JMenu:如上图蓝色边框
JMenuItem:如上图绿色字体处
其中JMenuBar是整体,一个界面中一般只有一个JMenuBar。
而JMenu是菜单中的选项,可以有多个。
JMenuItem是选项下面的条目,也可以有多个。
1,创建JMenuBar对象
2,创建JMenu对象
3,创建JMenuItem对象
4,把JMenuItem添加到JMenu中
5,把JMenu添加到JMenuBar中
6,把整个JMenuBar设置到整个界面中
代码示例:
//创建一个菜单对象
JMenuBar jMenuBar = new JMenuBar();
//设置菜单的宽高
jMenuBar.setSize(514, 20);
//创建一个选项
JMenu jMenu1 = new JMenu("功能");
//创建一个条目
jMenuItem1 = new JMenuItem("重新游戏");//把条目添加到选项当中
jMenu1.add(jMenuItem1);
//把选项添加到菜单当中
jMenuBar.add(jMenu1);
//把菜单添加到最外层的窗体当中
this.setJMenuBar(jMenuBar);

在上图中,其实是15张小图片。我们在添加图片的时候,要把添加图片的操作重复15次,才能把所有图片都添加到界面当中。
ImageIcon:描述图片的类,可以关联计算中任意位置的图片。
但是一般会把图片拷贝到当前项目中。
JLabel:用来管理图片,文字的类。
可以用来设置位置,宽高。

界面左上角的点可以看做是坐标的原点,横向的是X轴,纵向的是Y轴。
图片的位置其实取决于图片左上角的点,在坐标中的位置。
如果是(0,0)那么该图片会显示再屏幕的左上角。
1,取消整个界面的默认居中布局
2,创建ImageIcon对象,并制定图片位置。
3,创建JLabel对象,并把ImageIcon对象放到小括号中。
4,利用JLabel对象设置大小,宽高。
5,将JLabel对象添加到整个界面当中。
代码示例:
//1,先对整个界面进行设置//取消内部居中放置方式this.setLayout(null);
//2,创建ImageIcon对象,并制定图片位置。ImageIcon imageIcon1 = new ImageIcon("image\\1.png");
//3,创建JLabel对象,并把ImageIcon对象放到小括号中。JLabel jLabel1 = new JLabel(imageIcon1);
//4,利用JLabel对象设置大小,宽高。jLabel1.setBounds(0, 0, 100, 100);
//5,将JLabel对象添加到整个界面当中。this.add(jLabel1);
以此类推,只要能确定15张图片的位置,把上面的代码重复写15遍,就可以将所有图片都添加到界面中了。
每一张图片都对应1~15之间的数字,空白处为0,打乱图片实际上就是把数字打乱,添加图片的时候按照打乱的图片添加即可
int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
要求:打乱一维数组中的数据,并按照4个一组的方式添加到二维数组中。
代码示例:
public class Test1 {public static void main(String[] args) {//需求://把一个一维数组中的数据:0~15 打乱顺序//然后再按照4个一组的方式添加到二维数组当中//1.定义一个一维数组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;}//3.遍历数组for (int i = 0; i < tempArr.length; i++) {System.out.print(tempArr[i] + " ");}System.out.println();//4.创建一个二维数组int[][] data = new int[4][4];//5.给二维数组添加数据//解法一://遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {data[i / 4][i % 4] = tempArr[i];}//遍历二维数组for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[i].length; j++) {System.out.print(data[i][j] + " ");}System.out.println();}}
}
public class GameJFrame extends JFrame {//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}//4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon("C:\\Users\\moon\\IdeaProjects\\basic-code\\puzzlegame\\image\\animal\\animal3\\" + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j, 105 * i, 105, 105);//把管理容器添加到界面中this.getContentPane().add(jLabel);}}}private void initJMenuBar() {//创建整个的菜单对象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);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);}
}
事件是可以被组件识别的操作。
包含:
定义实现类实现ActionListener接口
public class Test3 {public static void main(String[] args) {JFrame jFrame = new JFrame();//设置界面的宽高jFrame.setSize(603, 680);//设置界面的标题jFrame.setTitle("事件演示");//设置界面置顶jFrame.setAlwaysOnTop(true);//设置界面居中jFrame.setLocationRelativeTo(null);//设置关闭模式jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件jFrame.setLayout(null);//创建一个按钮对象JButton jtb = new JButton("点我啊");//设置位置和宽高jtb.setBounds(0,0,100,50);//给按钮添加动作监听//jtb:组件对象,表示你要给哪个组件添加事件//addActionListener:表示我要给组件添加哪个事件监听(动作监听包含鼠标左键点击,空格)//参数:表示事件被触发之后要执行的代码jtb.addActionListener(new MyActionListener());//把按钮添加到界面当中jFrame.getContentPane().add(jtb);jFrame.setVisible(true);}
}public class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("按钮被点击了");}
}
public class Test3 {public static void main(String[] args) {JFrame jFrame = new JFrame();//设置界面的宽高jFrame.setSize(603, 680);//设置界面的标题jFrame.setTitle("事件演示");//设置界面置顶jFrame.setAlwaysOnTop(true);//设置界面居中jFrame.setLocationRelativeTo(null);//设置关闭模式jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件jFrame.setLayout(null);//创建一个按钮对象JButton jtb = new JButton("点我啊");//设置位置和宽高jtb.setBounds(0,0,100,50);//给按钮添加动作监听//jtb:组件对象,表示你要给哪个组件添加事件//addActionListener:表示我要给组件添加哪个事件监听(动作监听包含鼠标左键点击,空格)//参数:表示事件被触发之后要执行的代码jtb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("达咩~不要点我哟~");}});//把按钮添加到界面当中jFrame.getContentPane().add(jtb);jFrame.setVisible(true);}
}
public class MyJFrame extends JFrame implements ActionListener {//创建一个按钮对象JButton jtb1 = new JButton("点我啊");//创建一个按钮对象JButton jtb2 = new JButton("再点我啊");public MyJFrame(){//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给按钮设置位置和宽高jtb1.setBounds(0,0,100,50);//给按钮添加事件jtb1.addActionListener(this);//给按钮设置位置和宽高jtb2.setBounds(100,0,100,50);jtb2.addActionListener(this);//那按钮添加到整个界面当中this.getContentPane().add(jtb1);this.getContentPane().add(jtb2);//让整个界面显示出来this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {//对当前的按钮进行判断//获取当前被操作的那个按钮对象Object source = e.getSource();if(source == jtb1){jtb1.setSize(200,200);}else if(source == jtb2){Random r = new Random();jtb2.setLocation(r.nextInt(500),r.nextInt(500));}}
}
public void actionPerformed(ActionEvent e) 用于监听鼠标的动作,有单击,按下,释放,划入,划出动作
演示代码:
public class MyJFrame2 extends JFrame implements MouseListener {//创建一个按钮对象JButton jtb1 = new JButton("点我啊");public MyJFrame2(){//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给按钮设置位置和宽高jtb1.setBounds(0,0,100,50);//给按钮绑定鼠标事件jtb1.addMouseListener(this);//那按钮添加到整个界面当中this.getContentPane().add(jtb1);//让整个界面显示出来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("划出");}
}
用于监听键盘的动作,有点击,按下,松开,划入,划出动作
演示代码:
public class MyJFrame3 extends JFrame implements KeyListener {public MyJFrame3(){//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个窗体添加键盘监听//调用者this:本类对象,当前的界面对象,表示我要给整个界面添加监听//addKeyListener:表示要给本界面添加键盘监听//参数this:当事件被触发之后,会执行本类中的对应代码this.addKeyListener(this);//让整个界面显示出来this.setVisible(true);}@Overridepublic void keyTyped(KeyEvent e) {}//细节1://如果我们按下一个按键没有松开,那么会重复的去调用keyPressed方法//细节2://键盘里面那么多按键,如何进行区分?//每一个按键都有一个编号与之对应@Overridepublic void keyPressed(KeyEvent e) {System.out.println("按下不松");}@Overridepublic void keyReleased(KeyEvent e) {System.out.println("松开按键");//获取键盘上每一个按键的编号int code = e.getKeyCode();if(code == 65){System.out.println("现在按的是A");}else if(code == 66){System.out.println("现在按的是B");}}
}
1.将15张小图片移动到界面的中央偏下方
2.添加背景图片
细节:代码中后添加的,塞在下方
3.添加图片的边框
jLabe1.setBorder( new BevelBorder(1));
4.优化路径
从盘符开始的:绝对路径非盘符开始的:相对路径
实现代码:
public class GameJFrame extends JFrame {//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}//4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon("puzzlegame\\image\\animal\\animal3\\" + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);}private void initJMenuBar() {//创建整个的菜单对象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);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);}
}
1.本类实现KeyListener接口,并重写所有抽象方法
2.给整个界面添加键盘监听事件
3.统计一下空白方块对应的数字O在二维数组中的位置
4.在keyReleased方法当中实现移动的逻辑
心
5. Bug修复:
实现代码:
public class GameJFrame extends JFrame implements KeyListener{//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];int x;int y;int temp;String path = "puzzlegame\\image\\animal\\animal3\\";public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}//4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {if (tempArr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//清空原本已经出现的所有图片this.getContentPane().removeAll();//路径分为两种://绝对路径:一定是从盘符开始的。C:\ D:\//相对路径:不是从盘符开始的//相对路径相对当前项目而言的。 aaa\\bbb//在当前项目下,去找aaa文件夹,里面再找bbb文件夹。//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);this.getContentPane().repaint();}private void initJMenuBar() {//创建整个的菜单对象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);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {}//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");System.out.println(x+y);if(y == 3){return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if(x == 3){//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if(y == 0){return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if(x == 0){return;}//把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//调用方法按照最新的数字加载图片initImage();}}
}
按下a键显示完整图片
//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65){//把界面中所有的图片全部删除this.getContentPane().removeAll();//加载第一张完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83,134,420,420);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();
按下w直接胜利
//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");if(y == 3){return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if(x == 3){//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if(y == 0){return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if(x == 0){return;}//逻辑://把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();}else if(code == 65){initImage();}else if(code == 87){data = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};initImage();}}
实现步骤:
1.定义一个正确的二维数组win。
2.在加载图片之前,先判断一下二维数组中的数字跟win数组中是否相同。
3.如果相同展示正确图标4.如果不同则不展示正确图标
public class GameJFrame extends JFrame implements KeyListener {//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];//记录空白方块在二维数组中的位置int x = 0;int y = 0;//定义一个变量,记录当前展示图片的路径String path = "puzzlegame\\image\\animal\\animal3\\";//定义一个二维数组,存储正确的数据int[][] win = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义变量用来统计步数int step = 0;//创建选项下面的条目对象JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem accountItem = new JMenuItem("公众号");public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}//4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {if (tempArr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//清空原本已经出现的所有图片this.getContentPane().removeAll();if (victory()) {//显示胜利的图标JLabel winJLabel = new JLabel(new ImageIcon("puzzlegame\\image\\win.png"));winJLabel.setBounds(203, 283, 197, 73);this.getContentPane().add(winJLabel);}//路径分为两种://绝对路径:一定是从盘符开始的。C:\ D:\//相对路径:不是从盘符开始的//相对路径相对当前项目而言的。 aaa\\bbb//在当前项目下,去找aaa文件夹,里面再找bbb文件夹。//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新一下界面this.getContentPane().repaint();}private void initJMenuBar() {//创建整个的菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单上面的两个选项的对象 (功能 关于我们)JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");//将每一个选项下面的条目天极爱到选项当中functionJMenu.add(replayItem);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("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65) {if (victory()) {//结束方法return;}//把界面中所有的图片全部删除this.getContentPane().removeAll();//加载第一张完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83, 134, 420, 420);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}}//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//判断游戏是否胜利,如果胜利,此方法需要直接结束,不能再执行下面的移动代码了if (victory()) {//结束方法return;}//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");if (y == 3) {return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if (x == 3) {//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if (y == 0) {return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if (x == 0) {return;}//逻辑://把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//调用方法按照最新的数字加载图片initImage();} else if (code == 65) {initImage();} else if (code == 87) {data = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};initImage();}}//判断data数组中的数据是否跟win数组中相同//如果全部相同,返回true。否则返回falsepublic boolean victory() {for (int i = 0; i < data.length; i++) {//i : 依次表示二维数组 data里面的索引//data[i]:依次表示每一个一维数组for (int j = 0; j < data[i].length; j++) {if (data[i][j] != win[i][j]) {//只要有一个数据不一样,则返回falsereturn false;}}}//循环结束表示数组遍历比较完毕,全都一样返回truereturn true;}}
1.定义一个变量用来统计已经玩了多少步。
⒉.每次按上下左右的时候计步器自增一次即可。
public class GameJFrame extends JFrame implements KeyListener {//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];//记录空白方块在二维数组中的位置int x = 0;int y = 0;//定义一个变量,记录当前展示图片的路径String path = "puzzlegame\\image\\animal\\animal3\\";//定义一个二维数组,存储正确的数据int[][] win = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义变量用来统计步数int step = 0;//创建选项下面的条目对象JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem accountItem = new JMenuItem("公众号");public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}//4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {if (tempArr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//清空原本已经出现的所有图片this.getContentPane().removeAll();if (victory()) {//显示胜利的图标JLabel winJLabel = new JLabel(new ImageIcon("puzzlegame\\image\\win.png"));winJLabel.setBounds(203, 283, 197, 73);this.getContentPane().add(winJLabel);}JLabel stepCount = new JLabel("步数:" + step);stepCount.setBounds(50, 30, 100, 20);this.getContentPane().add(stepCount);//路径分为两种://绝对路径:一定是从盘符开始的。C:\ D:\//相对路径:不是从盘符开始的//相对路径相对当前项目而言的。 aaa\\bbb//在当前项目下,去找aaa文件夹,里面再找bbb文件夹。//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新一下界面this.getContentPane().repaint();}private void initJMenuBar() {//创建整个的菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单上面的两个选项的对象 (功能 关于我们)JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");//将每一个选项下面的条目天极爱到选项当中functionJMenu.add(replayItem);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("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65) {if (victory()) {//结束方法return;}//把界面中所有的图片全部删除this.getContentPane().removeAll();//加载第一张完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83, 134, 420, 420);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}}//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//判断游戏是否胜利,如果胜利,此方法需要直接结束,不能再执行下面的移动代码了if (victory()) {//结束方法return;}//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");if (y == 3) {return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if (x == 3) {//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if (y == 0) {return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if (x == 0) {return;}//逻辑://把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 65) {initImage();} else if (code == 87) {data = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};initImage();}}//判断data数组中的数据是否跟win数组中相同//如果全部相同,返回true。否则返回falsepublic boolean victory() {for (int i = 0; i < data.length; i++) {//i : 依次表示二维数组 data里面的索引//data[i]:依次表示每一个一维数组for (int j = 0; j < data[i].length; j++) {if (data[i][j] != win[i][j]) {//只要有一个数据不一样,则返回falsereturn false;}}}//循环结束表示数组遍历比较完毕,全都一样返回truereturn true;}}
1.给重新游戏绑定点击事件
2.重新打乱二维数组中的数字
3.加载图片
4.计步器清零
@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == replayItem){System.out.println("重新游戏");//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();}}
@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == reLoginItem){System.out.println("重新登录");//关闭当前的游戏界面this.setVisible(false);//打开登录界面new LoginJFrame();}}
@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == closeItem){System.out.println("关闭游戏");//直接关闭虚拟机即可System.exit(0);}}
@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == accountItem){System.out.println("公众号");//创建一个弹框对象JDialog jDialog = new JDialog();//创建一个管理图片的容器对象JLabelJLabel jLabel = new JLabel(new ImageIcon("puzzlegame\\image\\about.png"));//设置位置和宽高jLabel.setBounds(0,0,258,258);//把图片添加到弹框当中jDialog.getContentPane().add(jLabel);//给弹框设置大小jDialog.setSize(344,344);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭则无法操作下面的界面jDialog.setModal(true);//让弹框显示出来jDialog.setVisible(true);}}
public class GameJFrame extends JFrame implements KeyListener,ActionListener{//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];//记录空白方块在二维数组中的位置int x = 0;int y = 0;//定义一个变量,记录当前展示图片的路径String path = "puzzlegame\\image\\animal\\animal3\\";//定义一个二维数组,存储正确的数据int[][] win = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};//定义变量用来统计步数int step = 0;//创建选项下面的条目对象JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem accountItem = new JMenuItem("公众号");public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}/*** 5 6 8 9* 10 11 15 1* 4 7 12 13* 2 3 0 14** 5 6 8 9 10 11 15 1 4 7 12 13 2 3 0 14* *///4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {if (tempArr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//清空原本已经出现的所有图片this.getContentPane().removeAll();if (victory()) {//显示胜利的图标JLabel winJLabel = new JLabel(new ImageIcon("C:\\Users\\moon\\IdeaProjects\\basic-code\\puzzlegame\\image\\win.png"));winJLabel.setBounds(203,283,197,73);this.getContentPane().add(winJLabel);}JLabel stepCount = new JLabel("步数:" + step);stepCount.setBounds(50,30,100,20);this.getContentPane().add(stepCount);//路径分为两种://绝对路径:一定是从盘符开始的。C:\ D:\//相对路径:不是从盘符开始的//相对路径相对当前项目而言的。 aaa\\bbb//在当前项目下,去找aaa文件夹,里面再找bbb文件夹。//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新一下界面this.getContentPane().repaint();}private void initJMenuBar() {//创建整个的菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单上面的两个选项的对象 (功能 关于我们)JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");//将每一个选项下面的条目天极爱到选项当中functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);aboutJMenu.add(accountItem);//给条目绑定事件replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);accountItem.addActionListener(this);//将菜单里面的两个选项添加到菜单当中jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);//给整个界面设置菜单this.setJMenuBar(jMenuBar);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65){//把界面中所有的图片全部删除this.getContentPane().removeAll();//加载第一张完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83,134,420,420);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}}//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//判断游戏是否胜利,如果胜利,此方法需要直接结束,不能再执行下面的移动代码了if(victory()){//结束方法return;}//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");if(y == 3){return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if(x == 3){//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if(y == 0){return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if(x == 0){return;}//逻辑://把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();}else if(code == 65){initImage();}else if(code == 87){data = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};initImage();}}//判断data数组中的数据是否跟win数组中相同//如果全部相同,返回true。否则返回falsepublic boolean victory(){for (int i = 0; i < data.length; i++) {//i : 依次表示二维数组 data里面的索引//data[i]:依次表示每一个一维数组for (int j = 0; j < data[i].length; j++) {if(data[i][j] != win[i][j]){//只要有一个数据不一样,则返回falsereturn false;}}}//循环结束表示数组遍历比较完毕,全都一样返回truereturn true;}@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == replayItem){System.out.println("重新游戏");//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();}else if(obj == reLoginItem){System.out.println("重新登录");//关闭当前的游戏界面this.setVisible(false);//打开登录界面new LoginJFrame();}else if(obj == closeItem){System.out.println("关闭游戏");//直接关闭虚拟机即可System.exit(0);}else if(obj == accountItem){System.out.println("公众号");//创建一个弹框对象JDialog jDialog = new JDialog();//创建一个管理图片的容器对象JLabelJLabel jLabel = new JLabel(new ImageIcon("puzzlegame\\image\\about.png"));//设置位置和宽高jLabel.setBounds(0,0,258,258);//把图片添加到弹框当中jDialog.getContentPane().add(jLabel);//给弹框设置大小jDialog.setSize(344,344);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭则无法操作下面的界面jDialog.setModal(true);//让弹框显示出来jDialog.setVisible(true);}}
}
自行完成切换美女图片的功能。

1,在功能选项中添加更换图片,在更换图片里面再添加美女,动物,运动。
代码中功能是JMenu,更换图片也是JMenu,美女,动物,运动是三个JMenuItem
代码如下:
//创建菜单并添加到界面当中
//1.创建菜单JMenuBar的对象
JMenuBar jMenuBar = new JMenuBar();
//2.创建菜单上面的两个选项JMenu
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");//创建更换图片
JMenu changeImage = new JMenu("更换图片");//3.创建JMenuItem的对象
JMenuItem girl = new JMenuItem("美女");
JMenuItem animal = new JMenuItem("动物");
JMenuItem sport = new JMenuItem("运动");
JMenuItem repalyItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");//4.把美女,动物,运动添加到更换图片当中
changeImage.add(girl);
changeImage.add(animal);
changeImage.add(sport);//5.把更换图片,重新游戏,重新登录,关闭游戏添加到功能当中
functionJMenu.add(changeImage);
functionJMenu.add(repalyItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
//6.把公众号添加到关于我们当中
aboutJMenu.add(accountItem);//5.把功能,关于我们添加到JMenuBar当中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);//6.把整个菜单JMenuBar添加到整个界面当中
this.setJMenuBar(jMenuBar);
2,当我们点击了美女之后,就会从13组美女图片中随机选择一组。
3,当我们点击了动物之后,就会从8组动物图片中随机选择一组。
4,当我们点击了运动之后,就会从10组运动图片中随机选择一组。
5,细节1:选择完毕之后,游戏界面中需要加载所有的小图片并且打乱顺序
6,细节2:按A的时候显示的是选择之后的图片

public class GameJFrame extends JFrame implements KeyListener, ActionListener {//JFrame 界面,窗体//子类呢?也表示界面,窗体//规定:GameJFrame这个界面表示的就是游戏的主界面//以后跟游戏相关的所有逻辑都写在这个类中//创建一个二维数组//目的:用来管理数据//加载图片的时候,会根据二维数组中的数据进行加载int[][] data = new int[4][4];//记录空白方块在二维数组中的位置int x = 0;int y = 0;//定义一个变量,记录当前展示图片的路径String path = "puzzlegame\\image\\animal\\animal3\\";//定义一个二维数组,存储正确的数据int[][] win = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义变量用来统计步数int step = 0;//创建选项下面的条目对象JMenuItem girl = new JMenuItem("美女");JMenuItem animal = new JMenuItem("动物");JMenuItem sport = new JMenuItem("运动");JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem accountItem = new JMenuItem("公众号");//创建随机对象Random r = new Random();public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片(根据打乱之后的结果去加载图片)initImage();//让界面显示出来,建议写在最后this.setVisible(true);}//初始化数据(打乱)private void initData() {//1.定义一个一维数组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;}/*** 5 6 8 9* 10 11 15 1* 4 7 12 13* 2 3 0 14** 5 6 8 9 10 11 15 1 4 7 12 13 2 3 0 14* *///4.给二维数组添加数据//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组当中for (int i = 0; i < tempArr.length; i++) {if (tempArr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = tempArr[i];}}//初始化图片//添加图片的时候,就需要按照二维数组中管理的数据添加图片private void initImage() {//清空原本已经出现的所有图片this.getContentPane().removeAll();if (victory()) {//显示胜利的图标JLabel winJLabel = new JLabel(new ImageIcon("C:\\Users\\moon\\IdeaProjects\\basic-code\\puzzlegame\\image\\win.png"));winJLabel.setBounds(203, 283, 197, 73);this.getContentPane().add(winJLabel);}JLabel stepCount = new JLabel("步数:" + step);stepCount.setBounds(50, 30, 100, 20);this.getContentPane().add(stepCount);//路径分为两种://绝对路径:一定是从盘符开始的。C:\ D:\//相对路径:不是从盘符开始的//相对路径相对当前项目而言的。 aaa\\bbb//在当前项目下,去找aaa文件夹,里面再找bbb文件夹。//细节://先加载的图片在上方,后加载的图片塞在下面。//外循环 --- 把内循环重复执行了4次。for (int i = 0; i < 4; i++) {//内循环 --- 表示在一行添加4张图片for (int j = 0; j < 4; j++) {//获取当前要加载图片的序号int num = data[i][j];//创建一个JLabel的对象(管理容器)JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));//指定图片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//给图片添加边框//0:表示让图片凸起来//1:表示让图片凹下去jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//把管理容器添加到界面中this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新一下界面this.getContentPane().repaint();}private void initJMenuBar() {//创建整个的菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单上面的两个选项的对象 (功能 关于我们)JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");JMenu changeImage = new JMenu("更换图片");//把美女,动物,运动添加到更换图片当中changeImage.add(girl);changeImage.add(animal);changeImage.add(sport);//将更换图片,重新游戏,重新登录,关闭游戏添加到“功能”选项当中functionJMenu.add(changeImage);functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);//将公众号添加到关于我们当中aboutJMenu.add(accountItem);//绑定点击事件girl.addActionListener(this);animal.addActionListener(this);sport.addActionListener(this);replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);accountItem.addActionListener(this);//将菜单里面的两个选项添加到菜单当中jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);//给整个界面设置菜单this.setJMenuBar(jMenuBar);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松时会调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65) {//把界面中所有的图片全部删除this.getContentPane().removeAll();//加载第一张完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83, 134, 420, 420);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));background.setBounds(40, 40, 508, 560);//把背景图片添加到界面当中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}}//松开按键的时候会调用这个方法@Overridepublic void keyReleased(KeyEvent e) {//判断游戏是否胜利,如果胜利,此方法需要直接结束,不能再执行下面的移动代码了if (victory()) {//结束方法return;}//对上,下,左,右进行判断//左:37 上:38 右:39 下:40int code = e.getKeyCode();System.out.println(code);if (code == 37) {System.out.println("向左移动");if (y == 3) {return;}//逻辑://把空白方块右方的数字往左移动data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 38) {System.out.println("向上移动");if (x == 3) {//表示空白方块已经在最下方了,他的下面没有图片再能移动了return;}//逻辑://把空白方块下方的数字往上移动//x,y 表示空白方块//x + 1, y 表示空白方块下方的数字//把空白方块下方的数字赋值给空白方块data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 39) {System.out.println("向右移动");if (y == 0) {return;}//逻辑://把空白方块左方的数字往右移动data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 40) {System.out.println("向下移动");if (x == 0) {return;}//逻辑://把空白方块上方的数字往下移动data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;//每移动一次,计数器就自增一次。step++;//调用方法按照最新的数字加载图片initImage();} else if (code == 65) {initImage();} else if (code == 87) {data = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};initImage();}}//判断data数组中的数据是否跟win数组中相同//如果全部相同,返回true。否则返回falsepublic boolean victory() {for (int i = 0; i < data.length; i++) {//i : 依次表示二维数组 data里面的索引//data[i]:依次表示每一个一维数组for (int j = 0; j < data[i].length; j++) {if (data[i][j] != win[i][j]) {//只要有一个数据不一样,则返回falsereturn false;}}}//循环结束表示数组遍历比较完毕,全都一样返回truereturn true;}@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if (obj == replayItem) {System.out.println("重新游戏");//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();} else if (obj == reLoginItem) {System.out.println("重新登录");//关闭当前的游戏界面this.setVisible(false);//打开登录界面new LoginJFrame();} else if (obj == closeItem) {System.out.println("关闭游戏");//直接关闭虚拟机即可System.exit(0);} else if (obj == accountItem) {System.out.println("公众号");//创建一个弹框对象JDialog jDialog = new JDialog();//创建一个管理图片的容器对象JLabelJLabel jLabel = new JLabel(new ImageIcon("puzzlegame\\image\\about.png"));//设置位置和宽高jLabel.setBounds(0, 0, 258, 258);//把图片添加到弹框当中jDialog.getContentPane().add(jLabel);//给弹框设置大小jDialog.setSize(344, 344);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭则无法操作下面的界面jDialog.setModal(true);//让弹框显示出来jDialog.setVisible(true);} else if (obj == girl) {System.out.println("girl");//下列代码重复了,自己思考一下,能否抽取成一个方法呢?int number = r.nextInt(13) + 1;path = "puzzlegame\\image\\girl\\girl" + number + "\\";//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();} else if (obj == animal) {System.out.println("animal");//下列代码重复了,自己思考一下,能否抽取成一个方法呢?int number = r.nextInt(8) + 1;path = "puzzlegame\\image\\girl\\girl" + number + "\\";//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();} else if (obj == sport) {System.out.println("sport");//下列代码重复了,自己思考一下,能否抽取成一个方法呢?int number = r.nextInt(10) + 1;path = "puzzlegame\\image\\girl\\girl" + number + "\\";//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();}}}
独立完成登录界面,此题为附加扩展题。


public class LoginJFrame extends JFrame {//创建一个集合存储正确的用户名和密码static ArrayList list = new ArrayList<>();static {list.add(new User("zhangsan","123"));list.add(new User("lisi","1234"));}public LoginJFrame() {//初始化界面initJFrame();//在这个界面中添加内容initView();//让当前界面显示出来this.setVisible(true);}public void initView() {//1. 添加用户名文字JLabel usernameText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\用户名.png"));usernameText.setBounds(116, 135, 47, 17);this.getContentPane().add(usernameText);//2.添加用户名输入框JTextField username = new JTextField();username.setBounds(195, 134, 200, 30);this.getContentPane().add(username);//3.添加密码文字JLabel passwordText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\密码.png"));passwordText.setBounds(130, 195, 32, 16);this.getContentPane().add(passwordText);//4.密码输入框JTextField password = new JTextField();password.setBounds(195, 195, 200, 30);this.getContentPane().add(password);//验证码提示JLabel codeText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\验证码.png"));codeText.setBounds(133, 256, 50, 30);this.getContentPane().add(codeText);//验证码的输入框JTextField code = new JTextField();code.setBounds(195, 256, 100, 30);this.getContentPane().add(code);String codeStr = CodeUtil.getCode();JLabel rightCode = new JLabel();//设置内容rightCode.setText(codeStr);//位置和宽高rightCode.setBounds(300, 256, 50, 30);//添加到界面this.getContentPane().add(rightCode);//5.添加登录按钮JButton login = new JButton();login.setBounds(123, 310, 128, 47);login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按钮.png"));//去除按钮的默认边框login.setBorderPainted(false);//去除按钮的默认背景login.setContentAreaFilled(false);this.getContentPane().add(login);//6.添加注册按钮JButton register = new JButton();register.setBounds(256, 310, 128, 47);register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按钮.png"));//去除按钮的默认边框register.setBorderPainted(false);//去除按钮的默认背景register.setContentAreaFilled(false);this.getContentPane().add(register);//7.添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\login\\background.png"));background.setBounds(0, 0, 470, 390);this.getContentPane().add(background);}public void initJFrame() {this.setSize(488, 430);//设置宽高this.setTitle("拼图游戏 V1.0登录");//设置标题this.setDefaultCloseOperation(3);//设置关闭模式this.setLocationRelativeTo(null);//居中this.setAlwaysOnTop(true);//置顶this.setLayout(null);//取消内部默认布局}//要展示用户名或密码错误public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}
}
//设置位置和宽高
setBounds(x,y,宽,高);
//返回输入框中用户输入的数据
//细节:如果用户没有输入,返回的是一个长度为0的字符串
getText();
//修改数据
setText(要修改的内容);
//给按钮设置背景图片,方法中传递ImageIcon的对象即可
setIcon();
public class User {private String username;private String password;public User() {}public User(String username, String password) {this.username = username;this.password = password;}/*** 获取* @return username*/public String getUsername() {return username;}/*** 设置* @param username*/public void setUsername(String username) {this.username = username;}/*** 获取* @return password*/public String getPassword() {return password;}/*** 设置* @param password*/public void setPassword(String password) {this.password = password;}}
package com.itheima.util;import java.util.ArrayList;
import java.util.Random;public class CodeUtil {public static String getCode(){//1.创建一个集合ArrayList list = new ArrayList<>();//52 索引的范围:0 ~ 51//2.添加字母 a - z A - Zfor (int i = 0; i < 26; i++) {list.add((char)('a' + i));//a - zlist.add((char)('A' + i));//A - Z}//3.打印集合//System.out.println(list);//4.生成4个随机字母String result = "";Random r = new Random();for (int i = 0; i < 4; i++) {//获取随机索引int randomIndex = r.nextInt(list.size());char c = list.get(randomIndex);result = result + c;}//System.out.println(result);//长度为4的随机字符串//5.在后面拼接数字 0~9int number = r.nextInt(10);//6.把随机数字拼接到result的后面result = result + number;//System.out.println(result);//ABCD5//7.把字符串变成字符数组char[] chars = result.toCharArray();//[A,B,C,D,5]//8.在字符数组中生成一个随机索引int index = r.nextInt(chars.length);//9.拿着4索引上的数字,跟随机索引上的数字进行交换char temp = chars[4];chars[4] = chars[index];chars[index] = temp;//10.把字符数组再变回字符串String code = new String(chars);//System.out.println(code);return code;}
}
public class LoginJFrame extends JFrame implements MouseListener {static ArrayList allUsers = new ArrayList<>();static {allUsers.add(new User("zhangsan","123"));allUsers.add(new User("lisi","1234"));}JButton login = new JButton();JButton register = new JButton();JTextField username = new JTextField();//JTextField password = new JTextField();JPasswordField password = new JPasswordField();JTextField code = new JTextField();//正确的验证码JLabel rightCode = new JLabel();public LoginJFrame() {//初始化界面initJFrame();//在这个界面中添加内容initView();//让当前界面显示出来this.setVisible(true);}public void initView() {//1. 添加用户名文字JLabel usernameText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\用户名.png"));usernameText.setBounds(116, 135, 47, 17);this.getContentPane().add(usernameText);//2.添加用户名输入框username.setBounds(195, 134, 200, 30);this.getContentPane().add(username);//3.添加密码文字JLabel passwordText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\密码.png"));passwordText.setBounds(130, 195, 32, 16);this.getContentPane().add(passwordText);//4.密码输入框password.setBounds(195, 195, 200, 30);this.getContentPane().add(password);//验证码提示JLabel codeText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\验证码.png"));codeText.setBounds(133, 256, 50, 30);this.getContentPane().add(codeText);//验证码的输入框code.setBounds(195, 256, 100, 30);code.addMouseListener(this);this.getContentPane().add(code);String codeStr = CodeUtil.getCode();//设置内容rightCode.setText(codeStr);//绑定鼠标事件rightCode.addMouseListener(this);//位置和宽高rightCode.setBounds(300, 256, 50, 30);//添加到界面this.getContentPane().add(rightCode);//5.添加登录按钮login.setBounds(123, 310, 128, 47);login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按钮.png"));//去除按钮的边框login.setBorderPainted(false);//去除按钮的背景login.setContentAreaFilled(false);//给登录按钮绑定鼠标事件login.addMouseListener(this);this.getContentPane().add(login);//6.添加注册按钮register.setBounds(256, 310, 128, 47);register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按钮.png"));//去除按钮的边框register.setBorderPainted(false);//去除按钮的背景register.setContentAreaFilled(false);//给注册按钮绑定鼠标事件register.addMouseListener(this);this.getContentPane().add(register);//7.添加背景图片JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\login\\background.png"));background.setBounds(0, 0, 470, 390);this.getContentPane().add(background);}public void initJFrame() {this.setSize(488, 430);//设置宽高this.setTitle("拼图游戏 V1.0登录");//设置标题this.setDefaultCloseOperation(3);//设置关闭模式this.setLocationRelativeTo(null);//居中this.setAlwaysOnTop(true);//置顶this.setLayout(null);//取消内部默认布局}//点击@Overridepublic void mouseClicked(MouseEvent e) {if (e.getSource() == login) {System.out.println("点击了登录按钮");//获取两个文本输入框中的内容String usernameInput = username.getText();String passwordInput = password.getText();//获取用户输入的验证码String codeInput = code.getText();//创建一个User对象User userInfo = new User(usernameInput, passwordInput);System.out.println("用户输入的用户名为" + usernameInput);System.out.println("用户输入的密码为" + passwordInput);if (codeInput.length() == 0) {showJDialog("验证码不能为空");} else if (usernameInput.length() == 0 || passwordInput.length() == 0) {//校验用户名和密码是否为空System.out.println("用户名或者密码为空");//调用showJDialog方法并展示弹框showJDialog("用户名或者密码为空");} else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {showJDialog("验证码输入错误");} else if (contains(userInfo)) {System.out.println("用户名和密码正确可以开始玩游戏了");//关闭当前登录界面this.setVisible(false);//打开游戏的主界面//需要把当前登录的用户名传递给游戏界面new GameJFrame();} else {System.out.println("用户名或密码错误");showJDialog("用户名或密码错误");}} else if (e.getSource() == register) {System.out.println("点击了注册按钮");} else if (e.getSource() == rightCode) {System.out.println("更换验证码");//获取一个新的验证码String code = CodeUtil.getCode();rightCode.setText(code);}}public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}//按下不松@Overridepublic void mousePressed(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按下.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按下.png"));}}//松开按钮@Overridepublic void mouseReleased(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按钮.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按钮.png"));}}//鼠标划入@Overridepublic void mouseEntered(MouseEvent e) {}//鼠标划出@Overridepublic void mouseExited(MouseEvent e) {}//判断用户在集合中是否存在public boolean contains(User userInput){for (int i = 0; i < allUsers.size(); i++) {User rightUser = allUsers.get(i);if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword())){//有相同的代表存在,返回true,后面的不需要再比了return true;}}//循环结束之后还没有找到就表示不存在return false;}}
上一篇:《解构领域驱动设计》读书笔记