拼图小游戏———Day2

二.界面设置和菜单搭建

涉及知识:JFrame类方法   JMenuBer JMenu和JMenuItem类

界面的宽高

void setSize(int, int)

界面的标题

public void setTitle(String title )

界面置顶

public final void setAlwaysOnTop(boolean alwaysOnTop )

界面居中

public void setLocationRelativeTo(java.awt.Component c )

关闭模式

void

setDefaultCloseOperation(int operation)

Sets the operation that will happen by default when the user initiates a "close" on this frame.

显示

public void setVisible( boolean b)

JMenuBar、JMenu和JMenuItem是Swing中用于创建和管理菜单栏、菜单和菜单项的组件,它们在图形用户界面中扮演着重要的角色,使应用程序能够提供直观的操作和界面


设置

具体实现如下:

在这里可以看见关闭模式中int operation = 3的原因(三个界面注册,登入,游戏独立,不会同时出现);

public interface WindowConstants
{
    /**
     * The do-nothing default window close operation.
     */
    public static final int DO_NOTHING_ON_CLOSE = 0;

    /**
     * The hide-window default window close operation
     */
    public static final int HIDE_ON_CLOSE = 1;

    /**
     * The dispose-window default window close operation.
     * <p>
     * <b>Note</b>: When the last displayable window
     * within the Java virtual machine (VM) is disposed of, the VM may
     * terminate.  See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
     * AWT Threading Issues</a> for more information.
     * @see java.awt.Window#dispose()
     * @see JInternalFrame#dispose()
     */
    public static final int DISPOSE_ON_CLOSE = 2;

    /**
     * The exit application default window close operation. Attempting
     * to set this on Windows that support this, such as
     * <code>JFrame</code>, may throw a <code>SecurityException</code> based
     * on the <code>SecurityManager</code>.
     * It is recommended you only use this in an application.
     *
     * @since 1.4
     * @see JFrame#setDefaultCloseOperation
     */
    public static final int EXIT_ON_CLOSE = 3;

}

所以关闭模式参数可以写成


菜单制作


一个方法里面的代码太多了,若代码出现问题,很难快速查询到问题出处,所以我们需要将构造方法中不同功能的代码,抽取为独立的方法,因此可以将设置主界面,初始化功能放入不同的成员方法内,便于管理;

将下面的代码改为:

public GameJFrame(){
    //设置界面的宽高
    this.setSize(603,680);
    //设置界面的标题
    this.setTitle("拼图单机版 v1.0");
    //设置界面置顶
    this.setAlwaysOnTop(true);
    //设置界面居中
    this.setLocationRelativeTo(null);
    //设置关闭模式
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    //初始化菜单
    //创建整个菜单的对象
    JMenuBar jMenuBar = new JMenuBar();
    
    //创建菜单上面的两个选项的对象(功能   关于我们);
    JMenu founctionJMenu = new JMenu("功能");
    JMenu aboutJMenu = new JMenu("关于我们");
    
    //创建选项下面的条目对象(重新游戏   重新登录   关闭游戏)
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    //公众号
    JMenuItem accountItem = new JMenuItem("公众号");
    
    //将每一个选项下面的条目对象添加到选项当中
    founctionJMenu.add(replayItem);
    founctionJMenu.add(reLoginItem);
    founctionJMenu.add(closeItem);
    
    aboutJMenu.add(accountItem);
    
    //将菜单里的两个选项添加到菜单中
    jMenuBar.add(founctionJMenu);
    jMenuBar.add(aboutJMenu);
    
    //给整个界面设置菜单
    this.setJMenuBar(jMenuBar);
    
    //让显示出来,建议写在最后
    this.setVisible(true);
}

这样(在Idea 中 选中代码 按快捷键 (ctrl + alt + m)抽取为不同方法)

以后要是出现问题或要修改直接找到对应的方法

public class GameJFrame extends JFrame {
    //1.创建一个游戏的主界面
    //跟游戏相关的所有逻辑写在这个类中
    public GameJFrame(){
        //初始化界面
        initJFrame();
        //初始化菜单
        initJMenuBar();

        //让显示出来,建议写在最后
        this.setVisible(true);
    }

    private void initJMenuBar() {
        //创建整个菜单的对象
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上面的两个选项的对象(功能   关于我们);
        JMenu founctionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        //创建选项下面的条目对象(重新游戏   重新登录   关闭游戏)
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");
        //公众号
        JMenuItem accountItem = new JMenuItem("公众号");

        //将每一个选项下面的条目对象添加到选项当中
        founctionJMenu.add(replayItem);
        founctionJMenu.add(reLoginItem);
        founctionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        //将菜单里的两个选项添加到菜单中
        jMenuBar.add(founctionJMenu);
        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);
    }
}

结果展示

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值