数据结构小学期第三天

今天试着完成第二阶段的目标,实现九宫格拼图游戏,但是看着教程只有4*4的我也只能先按照这个做了

APP.class

 1 import com.itheima.ui.GameJFrame;
 2 import com.itheima.ui.LoginJFrame;
 3 import com.itheima.ui.RegisterJFrame;
 4 
 5 public class App {
 6     public static void main(String[] args) {
 7         //表示程序的启动入口
 8         //new LoginJFrame();
 9         new GameJFrame();
10         //new RegisterJFrame();
11     }
12 }

gameJframe.class

package com.itheima.ui;

import javax.swing.*;

public class GameJFrame extends JFrame {
    public GameJFrame(){
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //初始化图片
        initImage();

        //使界面显示出来
        this.setVisible(true);
    }

    private void initImage() {
        int number = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                //创建一个ImageIcon对象
                //创建一个JLabel对象(管理容器)
                JLabel jLabel = new JLabel(new ImageIcon("D:\\ideaworkplace\\puzzlegame\\image\\animal\\animal3\\"+number+".jpg"));
                //移动图片的坐标
                jLabel.setBounds(105*j,105*i,105,105);
                //把管理容器添加到界面当中
                //this.add(jLabel);
                this.getContentPane().add(jLabel);
                number++;
            }
        }
    }

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

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

        //创建选项中的条目
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公众号");

        //把条目放到选项当中
        functionMenu.add(replayItem);
        functionMenu.add(reLoginItem);
        functionMenu.add(closeItem);

        aboutMenu.add(accountItem);

        //把选项放到菜单中
        jMenuBar.add(functionMenu);
        jMenuBar.add(aboutMenu);

        //把菜单放到界面中
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //设置界面长宽
        this.setSize(603,680);
        //设置界面的标题
        this.setTitle("拼图游戏 v-1.0");
        //设置游戏总是置顶
        this.setAlwaysOnTop(true);
        //设置游戏界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //取消默认放置
        this.setLayout(null);
    }
}

LoginJframe.class

package com.itheima.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);
        //使界面显示出来
        this.setVisible(true);
    }
}

RegisterJframe.class

package com.itheima.ui;

import javax.swing.*;

public class RegisterJFrame extends JFrame {
    public RegisterJFrame(){
        this.setSize(488,500);
        this.setTitle("拼图注册");
        //设置游戏总是置顶
        this.setAlwaysOnTop(true);
        //设置游戏界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //使界面显示出来
        this.setVisible(true);
    }
}

后续代码正在实现稍后补上

posted @ 2024-07-03 16:26  新晋软工小白  阅读(11)  评论(0)    收藏  举报