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

📄 toplistdialog.java

📁 java编写的扫雷程序
💻 JAVA
字号:
/*
 * TopListDialog.java 1.0 2003-06-25
 *
 * Copyleft (c) 2003 RatKing.
 */

package jmine;

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

/**
 * “扫雷英雄榜”对话框
 *
 * @author <a href="ratking@ynet.com">RatKing</a>
 * @version 1.0
 */
public class TopListDialog extends JDialog {
    private JMine jmine;

    GridLayout gridLayout = new GridLayout(3, 0);
    JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
    JPanel levelPanel  = new JPanel(gridLayout);
    JPanel timePanel   = new JPanel(gridLayout);
    JPanel namePanel   = new JPanel(gridLayout);
    JPanel buttonPanel = new JPanel();

    JLabel gameLevelLabel1 = new JLabel("初级:");
    JLabel gameLevelLabel2 = new JLabel("中级:");
    JLabel gameLevelLabel3 = new JLabel("高级:");

    JLabel timeLabel1 = new JLabel();
    JLabel timeLabel2 = new JLabel();
    JLabel timeLabel3 = new JLabel();

    JLabel nameLabel1 = new JLabel();
    JLabel nameLabel2 = new JLabel();
    JLabel nameLabel3 = new JLabel();

    JButton resetButton = new JButton("重新计分(R)");
    JButton okButton = new JButton("确定(O)");

    private MineProps mineProps = JMine.mineProps;

    public TopListDialog(JMine jmine) {
        this(jmine, jmine.frame);
    }

    public TopListDialog(JMine jmine, Frame owner) {
        super(owner, "扫雷英雄榜", true);
        this.jmine = jmine;
        init();
    }

    private void init() {
        timeLabel1.setText(mineProps.getTime(MinePanel.LOW_LEVEL) + " 秒");
        timeLabel2.setText(mineProps.getTime(MinePanel.MIDDLE_LEVEL) + " 秒");
        timeLabel3.setText(mineProps.getTime(MinePanel.HIGH_LEVEL) + " 秒");
        timeLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
        timeLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
        timeLabel3.setHorizontalAlignment(SwingConstants.RIGHT);

        nameLabel1.setText(mineProps.getName(MinePanel.LOW_LEVEL));
        nameLabel2.setText(mineProps.getName(MinePanel.MIDDLE_LEVEL));
        nameLabel3.setText(mineProps.getName(MinePanel.HIGH_LEVEL));

        //JPanel p1 = new JPanel(new GridLayout(3, 3, 10, 2));
        //JPanel p2 = new JPanel();
        //p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));

        okButton.setMnemonic(KeyEvent.VK_O);
        resetButton.setMnemonic(KeyEvent.VK_R);
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //measure();
                dispose();
            }
        });
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });

        // 添加提示信息
        centerPanel.setToolTipText("<html><font size=3>对于每级游戏(初级、中级、高级),"
                                   + "<br>显示玩家的最短游戏时间,以及玩家的名称。</font></html>");
        resetButton.setToolTipText("<html><font size=3>清除当前的高分。</font></html>");
        okButton.setToolTipText("<html><font size=3>关闭该对话框并保存所作的更改。</font></html>");

        // 设置字体
        Font font = JMine.defaultFont;
        gameLevelLabel1.setFont(font);
        gameLevelLabel2.setFont(font);
        gameLevelLabel3.setFont(font);
        timeLabel1.setFont(font);
        timeLabel2.setFont(font);
        timeLabel3.setFont(font);
        nameLabel1.setFont(font);
        nameLabel2.setFont(font);
        nameLabel3.setFont(font);
        resetButton.setFont(font);
        okButton.setFont(font);

        // 开始组装
        levelPanel.add(gameLevelLabel1);
        levelPanel.add(gameLevelLabel2);
        levelPanel.add(gameLevelLabel3);

        timePanel.add(timeLabel1);
        timePanel.add(timeLabel2);
        timePanel.add(timeLabel3);

        namePanel.add(nameLabel1);
        namePanel.add(nameLabel2);
        namePanel.add(nameLabel3);

        centerPanel.add(levelPanel);
        centerPanel.add(timePanel);
        centerPanel.add(namePanel);

        buttonPanel.add(resetButton);
        buttonPanel.add(okButton);

        JPanel contentPane = (JPanel) getContentPane();
        contentPane.setBorder(BorderFactory.createEmptyBorder(20, 15, 10, 15));
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);

        getRootPane().setDefaultButton(okButton);  // 好像不灵! Bug?
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);

        pack();
        okButton.requestFocus();  // 这个方法灵!可以使“确定”按钮成为默认按钮
    }

    public void reset() {
        mineProps.resetTopList();

        timeLabel1.setText(mineProps.getTime(MinePanel.LOW_LEVEL) + " 秒");
        timeLabel2.setText(mineProps.getTime(MinePanel.MIDDLE_LEVEL) + " 秒");
        timeLabel3.setText(mineProps.getTime(MinePanel.HIGH_LEVEL) + " 秒");

        nameLabel1.setText(mineProps.getName(MinePanel.LOW_LEVEL));
        nameLabel2.setText(mineProps.getName(MinePanel.MIDDLE_LEVEL));
        nameLabel3.setText(mineProps.getName(MinePanel.HIGH_LEVEL));
    }
}

⌨️ 快捷键说明

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