JAVA 拼图小游戏(添加图片)

import javax.swing.*;

public class GameJFrame extends JFrame {
    //JFrame 界面,窗体
    //子类呢?也表示界面,窗体
    //规定:GameJFrame这个界面表示的就是游戏的主界面
    //以后跟游戏相关的所有逻辑都写在这个类中
    public GameJFrame(){
        //设置界面的宽高
        initJFrame();
        //设置菜单
        initJMenuBar();
        //设置图片
        initimage();

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

    }

    private void initimage() {
        int count = 1;
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                //创建一个图片ImageIcon的对象
                //ImageIcon icon = new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+count+".jpg");
                //创建一个Jlabel的对象(管理容器)
                JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+count+".jpg"));
                //指定图片的位置
                jLabel.setBounds(105*j,105*i,105,105);
                //把管理容器添加到界面中
                //this.add(jLabel);
                this.getContentPane().add(jLabel);
                count++;
            }
        }

    }

    private void initJMenuBar() {
        //创建整个的菜单对象
        JMenuBar jMenuBar = new JMenuBar();
        //创建菜单上面的两个选项的对象(功能 关于我们)
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");
        //创建选项下面的条目
        JMenuItem repalyItem = new JMenuItem("重新开始");
        JMenuItem reloginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公共号");
        //将每一个选项下面的条目集中到选项的中
        functionJMenu.add(repalyItem);
        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("拼图单机版 v.10");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置默认的关闭模式
        this.setDefaultCloseOperation(3);//0 表示DO_NOTHING_ON_CLOSE 通俗讲:关不掉
        //1 表示HIDE_ON_CLOSE 通俗讲:界面关闭但虚拟机未关闭
        //2 表示DISPOSE_ON_CLOSE 通俗讲:当开启多个界面的时候,只有把所有界面都关闭虚拟机才会停止。注意:所有界面都要设置成这个样子。
        //3 表示EXIT_ON_CLOSE 通俗讲: 关掉一个界面,虚拟机就停止了。
        //取消居中放置,按照xy轴的形式添加组件
        this.setLayout(null);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值