Bootstrap

JAVA扫雷代码

目录

1、前言

2、效果展示

 3、代码

3.1 Main

3.2 MineJFrame 主界面和菜单

 3.3 GridJLabel 数字和炸弹方格实现

 3.4 BasicGridButton 基本方格的点击事件的监听相关逻辑判断

3.5 笑脸按钮和计数计时功能实现

3.6 Win  弹出获胜窗口

3.7 DataClass 数据类

4、图片资源在github上。


1、前言

        去年土木大四寒假时,不想做毕设,学Java玩,很想做一个小游戏,但那时Java基础语法都还没学完,就找了个简单的来做,网上找了找,刚好找到了扫雷的图片资源,就做了个全图片的扫雷。菜得要命。还有本人英语一言难尽。虽然这注释少,我一年后(现在)还是看的懂,就不在添加注释了。有问题可以加讨论吹水群:332566785,也可以发邮件[email protected]

2、效果展示

中级模式

高级模式

胜利

失败

 3、代码

3.1 Main

public class Main {
    public static  void main(String[] args)
    {
        new MineJFrame();
    }
}

3.2 MineJFrame 主界面和菜单

import javax.swing.*;
import java.awt.*;

public  class MineJFrame extends JFrame {

    ImageIcon icon = new ImageIcon("image/icon.gif");
    Image imageIcon = icon.getImage();

    DataClass dataClass = new DataClass();
    FaceJLabel faceJLabel = new FaceJLabel(this);
    GridJLabel gridJLabel = new GridJLabel(faceJLabel);

    public MineJFrame() {
        setFrame();
        mineMenu();
        initframe();
        setVisible(true);
    }

    /*
     * 加载GridJPanel,设置JFrame尺寸
     */
    public void initframe(){
        //dataClass.reStart();
        add(faceJLabel);
        add(gridJLabel);
        setMinimumSize(new Dimension(DataClass.getMineRow()*16+16, DataClass.getMineRand()*16+104));
        repaint();
        pack();
    }
    /*
     * 重置
     */
    public void reStart(){
        dataClass.reStart();
        remove(gridJLabel);
        gridJLabel = new GridJLabel(faceJLabel);
        add(gridJLabel);
        faceJLabel.reSet();
    }
/*
*实现菜单
*/
    protected void mineMenu(){
        JMenuBar menuBar=new JMenuBar();
        JMenu menu = new JMenu("菜单");
        menu.setMargin(new Insets(0,0,0,0));//设置字边距;
        menu.addSeparator();//添加分割线;
        JMenu difficulty = new JMenu("难度");
        JMenuItem primary = new JMenuItem("初级");
        JMenuItem intermediate = new JMenuItem("中级");
        JMenuItem senior = new JMenuItem("高级");
        difficulty.addSeparator();//添加分割线;
        JMenu cheat = new JMenu("作弊模式");
        JMenuItem cheatOpen = new JMenuItem("开");
        JMenuItem cheatClose = new JMenuItem("关");

        primary.addActionListener(e -> {
            DataClass.setMineGrid(9,9,10);
            initframe();
            reStart();
            revalidate();

        });

        intermediate.addActionListener(e -> {
            DataClass.setMineGrid(16,16,40);
            initframe();
            reStart();

            validate();
        });

        senior.addActionListener(e -> {
            DataClass.setMineGrid(32,16,99);
            initframe();
            reStart();

            validate();
        });

        cheatOpen.addActionListener(e -> {
            DataClass.isCheat=true;
            initframe();
            reStart();
            validate();
        });

        cheatClose.addActionListener(e -> {
            DataClass.isCheat=false;
            initframe();
            reStart();
            validate();
        });

        difficulty.add(primary);
        difficulty.add(intermediate);
        difficulty.add(senior);

        cheat.add(cheatOpen);
        cheat.add(cheatClose);

        menu.add(difficulty);
        menu.add(cheat);
        menuBar.add(menu);
        setJMenuBar(menuBar);
    }

    public void setFrame(){
        setTitle("扫雷");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setIconImage(imageIcon);
        getContentPane().setBackground(new Color(191, 191, 191, 255));
        setResizable(false);
        setLocationRelativeTo(null);

        
;