• Java实现拼图小游戏(1)—— JFrame的认识及界面搭建


    本专栏收录了有关于拼图小游戏的文章,其中包括每一步的解析以及碰到的问题,拼图小游戏是Java的阶段项目,主要可以检测之前学习的有关于集合、条件判断语句、继承、接口等知识点的掌握情况,专栏地址:拼图小游戏(Java)

    一、前言

    本文主要是介绍JFrame以及简单的搭建界面

    二、主界面分析

    在这里插入图片描述

    三、创建主界面的框(测试类)

    1.步骤

    • 首先我们要新建对象
    • 设置界面的大小(长宽)
    • 设置界面可视化(如果没有这个步骤,屏幕上不会显示出来东西)
    • 设置游戏主界面、登录界面以及注册界面

    2.代码实现

    import javax.swing.*;
    
    public class Test {
        public static void main(String[] args) {
    
            //创建游戏主界面
            JFrame gameJFrame = new JFrame();
            //设置长宽
            gameJFrame.setSize(603,680);
            //设置可视化
            gameJFrame.setVisible(true);
    
            //创建登录界面
            JFrame loginJFrame = new JFrame();
            loginJFrame.setSize(488,430);
            loginJFrame.setVisible(true);
    
            //创建注册界面
            JFrame registerJFrame = new JFrame();
            registerJFrame.setSize(488,500);
            registerJFrame.setVisible(true);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3.测试代码

    在这里插入图片描述

    四、设置标题以及位置

    1.设置标题

    gameJFrame.setTitle("拼图小游戏 V1.0");
    
    • 1
    loginJFrame.setTitle("登录游戏");
    
    • 1
    registerJFrame.setTitle("注册");
    
    • 1

    2.设置界面位置

    设置永远置顶

    gameJFrame.setAlwaysOnTop(true);
    
    loginJFrame.setAlwaysOnTop(true);
    
    registerJFrame.setAlwaysOnTop(true);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    设置界面居中

    gameJFrame.setLocationRelativeTo(null);
    
    loginJFrame.setLocationRelativeTo(null);
    
    registerJFrame.setLocationRelativeTo(null);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.设置关闭模式

    此处如果对于关闭模式有疑问的话,可以点击下方链接查看

    JFrame中有关于DefaultCloseOperation的使用及参数说明(含源码阅读)

    gameJFrame.setDefaultCloseOperation(3);
    
    loginJFrame.setDefaultCloseOperation(3);
    
    registerJFrame.setDefaultCloseOperation(3);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    五、优化代码

    1.解释

    如果要在某一个界面里面添加功能的话,都在一个类中,会显得代码难以阅读,而且修改起来也会很困难,所以我们将游戏主界面、登录界面、以及注册界面都单独编成一个类,每一个类都继承JFrame父类,并且在类中创建方法来来实现页面

    2.游戏主界面

    import javax.swing.*;
    
    public class GameJFrame extends JFrame {
    
        public  GameJFrame(){
    
            //设置界面大小
            this.setSize(603,680);
    
            //设置标题
            this.setTitle("拼图小游戏 V1.0");
    
            //设置永远置顶
            this.setAlwaysOnTop(true);
    
            //设置程序随着窗口关闭而结束运行
            //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setDefaultCloseOperation(3);
    
            //设置界面居中
            this.setLocationRelativeTo(null);
    
            //设置界面可视化
            this.setVisible(true);
    	}
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3.登录界面

    import javax.swing.*;
    
    public class LoginJFrame extends JFrame {
    
        public LoginJFrame(){
    
            //设置界面大小
            this.setSize(488,430);
    
            //设置标题
            this.setTitle("登录游戏");
    
            //设置永远置顶
            this.setAlwaysOnTop(true);
    
            //设置程序随着窗口关闭而结束运行
            //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setDefaultCloseOperation(3);
    
            //设置界面居中
            this.setLocationRelativeTo(null);
    
            //设置界面可视化
            this.setVisible(true);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    4.注册界面

    import javax.swing.*;
    
    public class RegisterJFrame extends JFrame {
        public  RegisterJFrame(){
    
            //设置界面大小
            this.setSize(488,430);
    
            //设置标题
            this.setTitle("注册");
    
            //设置永远置顶
            this.setAlwaysOnTop(true);
    
            //设置程序随着窗口关闭而结束运行
            //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setDefaultCloseOperation(3);
    
            //设置界面居中
            this.setLocationRelativeTo(null);
    
            //设置界面可视化
            this.setVisible(true);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    六、结语

    下一篇文章会讲述有关菜单搭建的内容,如果在自己写代码的过程中遇到了问题可以留言评论

  • 相关阅读:
    Vue生命周期
    6.Docker搭建RabbitMQ
    flutter系列之:flutter中常用的GridView layout详解
    如何用windows上架ios到苹果商城
    论文解读:Rectifying the Shortcut Learning of Background for Few-Shot Learning
    java线程池详解
    攻防世界WEB练习-easyupload
    C++ | Leetcode C++题解之第326题3的幂
    【华为OD机试真题 JS】找朋友
    测试用例设计方法六脉神剑——第二剑:招式组合,因果判定出世
  • 原文地址:https://blog.csdn.net/Alita233_/article/details/126879887