⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 java版的扫雷,功能比较齐全
💻 JAVA
字号:
package clearminegame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

public class MainFrame extends JFrame {
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();
    JMenuBar menuBar = new JMenuBar();
    JMenu mnuFile = new JMenu();
    JMenuItem mnuExit = new JMenuItem();
    JMenu mnuHelp = new JMenu();
    JMenuItem mnuAbout = new JMenuItem();
    MinePanel pnlMine = new MinePanel();
    JCheckBoxMenuItem mnuHigh = new JCheckBoxMenuItem();
    JCheckBoxMenuItem mnuMiddle = new JCheckBoxMenuItem();
    JCheckBoxMenuItem mnuLower = new JCheckBoxMenuItem();
    ButtonGroup buttonGroupLevel = new ButtonGroup();
    JCheckBoxMenuItem mnuCustom = new JCheckBoxMenuItem();
    JMenuItem mnuContent = new JMenuItem();

    //自定义“雷区”大小的可选值
    private static Object [] rowSelectionValues;
    private static Object [] columnSelectionValues;
    static {
        //行数范围:9~24
        rowSelectionValues = new Object[16];
        for (int i = 0; i < rowSelectionValues.length; i++) {
            rowSelectionValues[i] = new Integer(i + 9);
        }
        //列数范围:9~30
        columnSelectionValues = new Object[22];
        for (int i = 0; i < columnSelectionValues.length; i++) {
            columnSelectionValues[i] = new Integer(i + 9);
        }
    }

    public MainFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        setTitle("扫雷");
        mnuFile.setMnemonic('F');
        mnuFile.setText("文件(F)");
        mnuExit.setMnemonic('E');
        mnuExit.setText("退出(E)");
        mnuExit.addActionListener(new
                                        MainFrame_jMenuFileExit_ActionAdapter(this));
        mnuHelp.setMnemonic('H');
        mnuHelp.setText("帮助(H)");
        mnuAbout.setMnemonic('A');
        mnuAbout.setText("关于(A)");
        mnuAbout.addActionListener(new
                                         MainFrame_jMenuHelpAbout_ActionAdapter(this));
        mnuHigh.setSelected(true);
        mnuHigh.setText("高级(H)");
        mnuHigh.addActionListener(new MainFrame_mnuHigh_actionAdapter(this));
        mnuMiddle.setText("中级(M)");
        mnuMiddle.addActionListener(new MainFrame_mnuMiddle_actionAdapter(this));
        mnuLower.setText("初级(L)");
        mnuLower.addActionListener(new MainFrame_mnuLower_actionAdapter(this));
        mnuCustom.setText("自定义(C)...");
        mnuCustom.addActionListener(new MainFrame_mnuCustom_actionAdapter(this));
        mnuContent.setMnemonic('C');
        mnuContent.setText("帮助内容(C)");
        mnuContent.addActionListener(new MainFrame_mnuContent_actionAdapter(this));
        menuBar.add(mnuFile);
        mnuFile.add(mnuLower);
        mnuFile.add(mnuMiddle);
        mnuFile.add(mnuHigh);
        mnuFile.addSeparator();
        mnuFile.add(mnuCustom);
        mnuFile.addSeparator();
        mnuFile.add(mnuExit);
        menuBar.add(mnuHelp);
        mnuHelp.add(mnuContent);
        mnuHelp.addSeparator();
        mnuHelp.add(mnuAbout);
        setJMenuBar(menuBar);
        //
        mnuContent.setAccelerator(KeyStroke.getKeyStroke("F1"));
        buttonGroupLevel.add(mnuLower);
        buttonGroupLevel.add(mnuMiddle);
        buttonGroupLevel.add(mnuHigh);
        buttonGroupLevel.add(mnuCustom);
        //
        contentPane.add(pnlMine);
        setResizable(false);
        //
        mnuHigh.doClick();
    }

    private void start(int rowCount, int columnCount, int mineCount)
    {
        pnlMine.startNew(rowCount, columnCount, mineCount);
        this.pack();
    }

    /**
     * File | Exit action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
        System.exit(0);
    }

    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
        MainFrame_AboutBox dlg = new MainFrame_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.pack();
        dlg.setVisible(true);
    }

    public void mnuLower_actionPerformed(ActionEvent e) {
        start(9, 9, 10);
    }

    public void mnuMiddle_actionPerformed(ActionEvent e) {
        start(16, 16, 40);
    }

    public void mnuHigh_actionPerformed(ActionEvent e) {
        start(16, 30, 99);
    }

    public void mnuCustom_actionPerformed(ActionEvent e) {
        //
        Object obj = JOptionPane.showInputDialog(this, "请选择行数", "自定义", JOptionPane.QUESTION_MESSAGE, null, rowSelectionValues, new Integer(pnlMine.getRowCount()));
        if( ! (obj instanceof Integer ) )
            return;
        int rowCount = ((Integer)obj).intValue();
        //
        obj = JOptionPane.showInputDialog(this, "请选择列数", "自定义", JOptionPane.QUESTION_MESSAGE, null, columnSelectionValues,  new Integer(pnlMine.getColumnCount()));
        if( ! (obj instanceof Integer ) )
            return;
        int columnCount = ((Integer)obj).intValue();
        //
        int minMineCount = 10;
        int maxMineCount = (rowCount-1)*(columnCount-1);//最大雷数,计算方式与Windows的一模一样
        //
        Object[] selectionValues = new Object[maxMineCount-minMineCount+1];
        for (int i = 0; i < selectionValues.length; i++) {
            selectionValues[i] = new Integer(minMineCount);
            minMineCount ++;
        }
        //
        obj = JOptionPane.showInputDialog(this, "请选择雷数", "自定义", JOptionPane.QUESTION_MESSAGE, null, selectionValues, new Integer(pnlMine.getMineCount()));
        if( ! (obj instanceof Integer ) )
            return;
        int mineCount = ((Integer)obj).intValue();
        //
        start(rowCount, columnCount, mineCount);
    }

    public void mnuContent_actionPerformed(ActionEvent e) {
        String content = "\n操作说明:\n单击左键翻开方块。方块上的数字代表其周围8个方块中的雷的数量\n根据数字进行判断,如果确定某个方块为雷,则单击右键给方块加上“!”记号\n如果认为某个方块有可能为雷但不完全确定,可双击右键加上“?”标记,防止不小心点中而触雷\n已标记“?”号的方块,再次单击右键可恢复正常状态\n已翻开的方块,如果其数字与周围的“!”的数量相等(意味着周围的雷都已找到),则双击该方块可快速翻开其周围的剩余方块";
        JOptionPane.showMessageDialog(this, content, "帮助", JOptionPane.INFORMATION_MESSAGE);
    }
}


class MainFrame_mnuContent_actionAdapter implements ActionListener {
    private MainFrame adaptee;
    MainFrame_mnuContent_actionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.mnuContent_actionPerformed(e);
    }
}


class MainFrame_mnuCustom_actionAdapter implements ActionListener {
    private MainFrame adaptee;
    MainFrame_mnuCustom_actionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.mnuCustom_actionPerformed(e);
    }
}


class MainFrame_mnuHigh_actionAdapter implements ActionListener {
    private MainFrame adaptee;
    MainFrame_mnuHigh_actionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.mnuHigh_actionPerformed(e);
    }
}


class MainFrame_mnuMiddle_actionAdapter implements ActionListener {
    private MainFrame adaptee;
    MainFrame_mnuMiddle_actionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.mnuMiddle_actionPerformed(e);
    }
}


class MainFrame_mnuLower_actionAdapter implements ActionListener {
    private MainFrame adaptee;
    MainFrame_mnuLower_actionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.mnuLower_actionPerformed(e);
    }
}


class MainFrame_jMenuFileExit_ActionAdapter implements ActionListener {
    MainFrame adaptee;

    MainFrame_jMenuFileExit_ActionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuFileExit_actionPerformed(actionEvent);
    }
}


class MainFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
    MainFrame adaptee;

    MainFrame_jMenuHelpAbout_ActionAdapter(MainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -