2023/5/31总结

跟着黑马程序员写小项目:拼图游戏

今天实现了窗口的弹出

明白了整个项目的大概框架,和一些基本的语法

代码如下:

//APP
package ui;

public class App {
    public static void main(String[] args) {
        //程序的启动路口

        //如果想要开启一个界面,就创建谁的对象即可
        //new LoginJFrame();

        //new RegisterJFrame();

        new GameJFrame();
    }
}


//GameJFrame
package ui;

import javax.swing.*;

public class GameJFrame extends JFrame {
    //JFrame 界面,窗体
    //子类也表示界面,窗体
    //规定:GameJFrame这个界面表示游戏主界面
    //构造方法:
    public GameJFrame(){
        initJFrame();//ctrl+alt+m:创建构造方法

        initJMenuBar();

        InitImage();

        this.setVisible(true);
    }


    //初始化图片
    private void InitImage() {
        //创建一个图片ImageIcon的对象
        ImageIcon icon=new ImageIcon("D:\\java\\PTgame\\image\\animal\\animal3\\1.jpg");

        //创建一个JLable的对象(管理容器)
        JLabel jLabel=new JLabel(icon);

        //指定图片位置
        jLabel.setBounds(0,0,105,105);

        //把管理容器添加到界面中
        //this.add(jLabel);
        //获取隐藏的容器
        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() {//ctrl+b跟进跳转到方法
        this.setSize(603,680);
        //界面标题
        this.setTitle("拼图游戏单机版 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束

        //取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
    }
}

//LoginJFrame
package ui;

import javax.swing.*;

public class LoginJFrame extends JFrame {
    //登入界面

    public LoginJFrame(){
        this.setSize(488,430);

        this.setTitle("拼图 登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束

        this.setVisible(true);
    }
}

//RegisterJFrame
package ui;

import javax.swing.*;

public class LoginJFrame extends JFrame {
    //登入界面

    public LoginJFrame(){
        this.setSize(488,430);

        this.setTitle("拼图 登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束

        this.setVisible(true);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值