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

📄 jmine.java

📁 用java实现的全真模拟windows下的扫雷程序。不过目前实现以button方式实现。改进可以考虑以位图形式实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                rows = dlg.getRows();
                columns = dlg.getColumns();
                mines = dlg.getMines();

                minePanel.setGameLevel(rows, columns, mines);

                if (isStandalone) {
                    if (frame != null) {
                        //frame.pack();
                        Utils.packPlaceShow(frame);
                    }
                }
                else {
                    setSize(getPreferredSize());
                    validate();
                }
            }
        });

        miMark.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
                minePanel.isMark = state;
                mineProps.isMark = state;
            }
        });
        miColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
                minePanel.isColor = state;
                mineProps.isColor = state;
                // 暂未实现
            }
        });
        miSound.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
                minePanel.isSound = state;
                mineProps.isSound = state;
                // 实现不完全,只能发出单调的beep声
            }
        });

        miTopList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showTopListDialog();
            }
        });

        if (isStandalone) {
            miExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    exitApp();
                }
            });
        }
        else {
            miExit.setEnabled(false);
        }

        miAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showAboutDialog();
            }
        });

        // 组装菜单
        mFile.add(miNew);
        mFile.addSeparator();
        mFile.add(miLowRank);
        mFile.add(miMiddleRank);
        mFile.add(miHighRank);
        mFile.add(miCustomRank);
        mFile.addSeparator();
        mFile.add(miMark);
        mFile.add(miColor);
        mFile.add(miSound);
        mFile.addSeparator();
        mFile.add(miTopList);
        mFile.addSeparator();
        mFile.add(miExit);

        mHelp.add(miContent);
        mHelp.add(miSearch);
        mHelp.add(miShowHelp);
        mHelp.addSeparator();
        mHelp.add(miAbout);

        menuBar.add(mFile);
        menuBar.add(mHelp);

        // 重新设置菜单的字体
        // Font font = new Font(mFile.getFont().getName(), Font.PLAIN, 12);
        mFile.setFont(defaultFont);
        mHelp.setFont(defaultFont);
        miNew.setFont(defaultFont);
        miLowRank.setFont(defaultFont);
        miMiddleRank.setFont(defaultFont);
        miHighRank.setFont(defaultFont);
        miCustomRank.setFont(defaultFont);
        miMark.setFont(defaultFont);
        miColor.setFont(defaultFont);
        miSound.setFont(defaultFont);
        miTopList.setFont(defaultFont);
        miExit.setFont(defaultFont);
        miContent.setFont(defaultFont);
        miSearch.setFont(defaultFont);
        miShowHelp.setFont(defaultFont);
        miAbout.setFont(defaultFont);

        // 返回菜单栏
        return menuBar;
    }

    /**
     * Creates an icon from an image contained in the "images" directory.
     */
    public ImageIcon createImageIcon(String filename, String description) {
	String path = "/images/" + filename;
	return new ImageIcon(getClass().getResource(path), description);
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        // 保存属性
        if (isStandalone) {
            mineProps.save();
        }

        stop();
        destroy();

        if (frame != null)
            frame.dispose();
        if (isStandalone)
            System.exit(0);
    }

    /**
     * 显示“扫雷英雄榜”对话框
     */
    public void showTopListDialog() {
        TopListDialog dlg = new TopListDialog(this);
        //dlg.show();
        Utils.packCenterShow(frame, dlg);
    }

    /**
     * 显示“关于”对话框
     */
    public void showAboutDialog() {
        JDialog dlg = new AboutDialog(this);
        //dlg.show();
        Utils.packCenterShow(frame, dlg);
    }

    /** 作为本Java Application的主方法 */
    public static void main(String[] args) {
        final JMine jMine = new JMine();
        jMine.isStandalone = true;

        /*final */JFrame frame = new JFrame("扫雷");
        jMine.frame = frame;

        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jMine.exitApp();
            }
            public void windowDeiconified(WindowEvent e) {
                jMine.start();
            }
            public void windowIconified(WindowEvent e) {
                jMine.stop();
            }
        });

        ImageIcon imgIcon = jMine.createImageIcon("mine.png", "地雷图标");
        frame.setIconImage(imgIcon.getImage());

        frame.getContentPane().add(jMine, BorderLayout.CENTER);
        jMine.init();
        jMine.start();

//        frame.setUndecorated(true);
//        frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
//        SwingUtilities.updateComponentTreeUI(frame.getRootPane());

        // 包装/缩紧窗口
        frame.pack();
        // 使窗口居于屏幕中央
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2,
                          (d.height - frame.getSize().height) / 2);
        // 显示窗口
        frame.setVisible(true);
    }

    //static initializer for setting look & feel
    static {
        try {
            String vers = System.getProperty("java.version");
            if (vers.compareTo("1.4") < 0) {
                System.out.println("JMine must be run with a 1.4 or higher version JVM!!!");
                System.exit(0);
            }
        } catch (Throwable t) {
            System.out.println("uncaught exception: " + t);
            t.printStackTrace();
        }

        // 载入属性
        mineProps = new MineProps();
        mineProps.load();

    	// 决定外观感觉
        try {
            // javax.swing.plaf.metal.MetalLookAndFeel
            if (mineProps.lookAndFeel.equals(MineProps.METAL)) {
    	        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            }
            else if (mineProps.lookAndFeel.equals(MineProps.WINDOZ)) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
            else if (mineProps.lookAndFeel.equals(MineProps.MOTIF)) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            }
            else {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
    	} catch (Exception exc) {
    	    System.err.println("载入界面样式失败:" + exc);
    	}
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	JDialog.setDefaultLookAndFeelDecorated(true);
    }
}

⌨️ 快捷键说明

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