📄 mineframe.java
字号:
package com.by.brt.mine;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.Timer;
/**
* 主窗口类.
*
* @author brt
*
*/
public class MineFrame extends JFrame {
// 菜单栏
private JMenuBar mb;
// 菜单项
private JMenu mugame, muhelp;
// 菜单项子项
private JMenuItem mi_start, mi_hero, mi_exit, mi_copyright;
private JRadioButtonMenuItem mi_easy, mi_normal, mi_hard, mi_setuself;
private ButtonGroup btngroup;
private JPanel temp, jpup, jpdown;
// 剩余雷数,时间,重新开始按键
private JLabel mine_num_hundred, mine_num_ten, mine_num_bit,
time_num_hundred, time_num_ten, time_num_bit, jRestart;
private boolean gameover, timer_start, timer_stop;
// 格子显示的初始状态和雷的各种形态
private Icon[] iconmine = { new ImageIcon("./image/mine.gif"),
new ImageIcon("./image/blood.gif"),
new ImageIcon("./image/blank.gif"),
new ImageIcon("./image/error.gif") };
// 标记状态显示
private Icon[] iconmarks = { new ImageIcon("./image/flag.gif"),
new ImageIcon("./image/ask.gif") };
// 雷区数字显示
private Icon[] iconnum = { new ImageIcon("./image/0.gif"),
new ImageIcon("./image/1.gif"), new ImageIcon("./image/2.gif"),
new ImageIcon("./image/3.gif"), new ImageIcon("./image/4.gif"),
new ImageIcon("./image/5.gif"), new ImageIcon("./image/6.gif"),
new ImageIcon("./image/7.gif"), new ImageIcon("./image/8.gif"), };
// 按钮各种脸部表情
private Icon[] face = { new ImageIcon("./image/face1.gif"),
new ImageIcon("./image/face2.gif"),
new ImageIcon("./image/face3.gif"),
new ImageIcon("./image/face4.gif"),
new ImageIcon("./image/face5.gif") };
// 红色液晶效果数字图片
private Icon[] lednum = { new ImageIcon("./image/d0.gif"),
new ImageIcon("./image/d1.gif"), new ImageIcon("./image/d2.gif"),
new ImageIcon("./image/d3.gif"), new ImageIcon("./image/d4.gif"),
new ImageIcon("./image/d5.gif"), new ImageIcon("./image/d6.gif"),
new ImageIcon("./image/d7.gif"), new ImageIcon("./image/d8.gif"),
new ImageIcon("./image/d9.gif"), new ImageIcon("./image/d10.gif") };
//
private int rows = 9, cols = 10, minesnum = 10, markcount = 0, time = 0;
private int level = 1;
private Grid[][] grid;
private MineListener ml = new MineListener();
private Timer timer = new Timer(1000, new TimerAL());
/**
* 主Frame的构造函数初始化菜单栏和界面.
*
*/
public MineFrame() {
// 添加菜单栏
mb = new JMenuBar();
mugame = new JMenu("游戏(G)");
muhelp = new JMenu("帮助(H)");
mi_start = new JMenuItem("开局");
mi_start.addMouseListener(ml);
mi_easy = new JRadioButtonMenuItem("初级");
mi_easy.addMouseListener(ml);
mi_normal = new JRadioButtonMenuItem("中级");
mi_normal.addMouseListener(ml);
mi_hard = new JRadioButtonMenuItem("高级");
mi_hard.addMouseListener(ml);
mi_setuself = new JRadioButtonMenuItem("自定义");
mi_setuself.addMouseListener(ml);
btngroup = new ButtonGroup();
btngroup.add(mi_easy);
btngroup.add(mi_normal);
btngroup.add(mi_hard);
btngroup.add(mi_setuself);
mi_hero = new JMenuItem("扫雷英雄榜");
mi_hero.addMouseListener(ml);
mi_exit = new JMenuItem("退出");
mi_exit.addMouseListener(ml);
mi_copyright = new JMenuItem("版权信息");
mi_copyright.addMouseListener(ml);
mugame.add(mi_start);
mugame.addSeparator();// 添加分隔线
mugame.add(mi_easy);
mugame.add(mi_normal);
mugame.add(mi_hard);
mugame.add(mi_setuself);
mugame.addSeparator();
mugame.add(mi_hero);
mugame.addSeparator();
mugame.add(mi_exit);
muhelp.add(mi_copyright);
muhelp.addSeparator();
mb.add(mugame);
mb.add(muhelp);
this.setJMenuBar(mb);// 在窗口中添加菜单栏
this.setTitle("扫雷");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭
this.setResizable(false);
jpdown = new JPanel(new FlowLayout(1, 0, 5));
jpdown.setBorder(BorderFactory.createRaisedBevelBorder());
jpdown.setBackground(Color.LIGHT_GRAY);
setStatusPanel();
setGridPanel();
this.setVisible(true);
}
/*
* 设置上部面板
*/
private void setStatusPanel() {
jpup = new JPanel(new FlowLayout(1, 13, 5));
jpup.setBorder(BorderFactory.createRaisedBevelBorder());
jpup.setBackground(Color.LIGHT_GRAY);
temp = new JPanel(new FlowLayout(1, 0, 0));
temp.setBackground(Color.LIGHT_GRAY);
temp.setBorder(BorderFactory.createLoweredBevelBorder());
mine_num_hundred = new JLabel(lednum[(minesnum - markcount) / 100]);// 剩余雷数
mine_num_ten = new JLabel(lednum[(minesnum - markcount) % 100 / 10]);
mine_num_bit = new JLabel(lednum[(minesnum - markcount) % 10]);
temp.add(mine_num_hundred);
temp.add(mine_num_ten);
temp.add(mine_num_bit);
jpup.add(temp);
temp = new JPanel(new FlowLayout(1, 0, 0));
temp.setBackground(Color.LIGHT_GRAY);
jRestart = new JLabel(face[0]);// 重新开始
jRestart.addMouseListener(ml);
temp.add(jRestart);
jpup.add(temp);
temp = new JPanel(new FlowLayout(1, 0, 0));
temp.setBackground(Color.LIGHT_GRAY);
temp.setBorder(BorderFactory.createLoweredBevelBorder());
time_num_hundred = new JLabel(lednum[0]);// 时间计数
time_num_ten = new JLabel(lednum[0]);
time_num_bit = new JLabel(lednum[0]);
temp.add(time_num_hundred);
temp.add(time_num_ten);
temp.add(time_num_bit);
jpup.add(temp);
this.getContentPane().add(jpup, BorderLayout.NORTH);
reShowLeftStatus();
reShowTime();
this.setVisible(true);
}
/*
* 设置下部面板
*/
void setGridPanel() {
jpdown.removeAll();
if (level == 1) {
rows = 9;
cols = 9;
minesnum = 10;
} else if (level == 2) {
rows = 16;
cols = 16;
minesnum = 40;
} else if (level == 3) {
rows = 16;
cols = 30;
minesnum = 99;
} else {
rows = this.getRows();
cols = this.getCols();
minesnum = this.getMinesnum();
}
// 添加雷区格子
JPanel tempGrid = new JPanel(new GridLayout(rows, cols, 0, 0));
tempGrid.setBorder(BorderFactory.createLoweredBevelBorder());
grid = new Grid[rows][cols];// 创建这么多个Grid类型的数组
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = new Grid(i, j);// 每一个grid都new一个Grid
grid[i][j].setIcon(iconmine[2]);
grid[i][j].addMouseListener(ml);
tempGrid.add(grid[i][j]);
}
}
jpdown.add(tempGrid);
this.setSize(cols * 19, rows * 18 + 100);//
this.setLocationRelativeTo(null);
this.getContentPane().add(jpdown, BorderLayout.CENTER);
this.setVisible(true);
initGame();
}
/*
* 重新开始方法
*/
private void initGame() {
gameover = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j].setShownum(0);
grid[i][j].setCanclick(true);
grid[i][j].setMine(false);
grid[i][j].setMark(false);
grid[i][j].setExpand(false);
grid[i][j].setQuestion(false);
grid[i][j].setIcon(iconmine[2]);
}
}
minesnum = this.getMinesnum();
markcount = 0;
reShowLeftStatus();
jRestart.setIcon(face[0]);
timer.stop();
time = 0;
timer_start = false;
timer_stop = false;
reShowTime();
this.setMine();
this.setGridShowNum();
}
/*
* 游戏胜利
*/
private void winGame() {
int temp = 0, count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j].isMine() && grid[i][j].isMark()) {
temp++;
} else if (!grid[i][j].isMine() && grid[i][j].isExpand()) {
count++;
}
}
}
if (count == rows * cols - minesnum) {
endGame();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j].isMine() && !grid[i][j].isExpand()) {
grid[i][j].setIcon(iconmarks[0]);
grid[i][j].isMark();
}
}
}
jRestart.setIcon(face[3]);
markcount = minesnum;
reShowLeftStatus();
if (this.level != 0) {
new WhiteNameDialog();
}
} else if (count == rows * cols - minesnum && minesnum == temp) {
endGame();
jRestart.setIcon(face[3]);
if (this.level != 0) {
new WhiteNameDialog();
}
}
}
/*
* 游戏结束
*/
private void endGame() {
gameover = true;
timer.stop();
timer_start = false;
timer_stop = true;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j].setCanclick(false);
if (!grid[i][j].isExpand()) {
if (grid[i][j].isMark() && !grid[i][j].isMine()) {
grid[i][j].setIcon(iconmine[3]);
}
if (grid[i][j].isMine() && !grid[i][j].isMark()) {
grid[i][j].setIcon(iconmine[0]);
}
}
}
}
jRestart.setIcon(face[2]);
}
/*
* 重新显示状态栏,剩余雷数
*/
private void reShowLeftStatus() {
if (minesnum >= markcount) {
mine_num_hundred.setIcon(lednum[(minesnum - markcount) / 100]);
mine_num_ten.setIcon(lednum[(minesnum - markcount) % 100 / 10]);
mine_num_bit.setIcon(lednum[(minesnum - markcount) % 10]);
} else {
mine_num_hundred.setIcon(lednum[10]);
mine_num_ten.setIcon(lednum[(markcount - minesnum) % 100 / 10]);
mine_num_bit.setIcon(lednum[(markcount - minesnum) % 10]);
}
}
/*
* 重新显示状态栏,时间计数
*/
private void reShowTime() {
time_num_hundred.setIcon(lednum[time / 100]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -