Repository navigation
Expand file tree
/
Copy pathMinesFrame.java
More file actions
69 lines (55 loc) · 1.59 KB
/
Copy pathMinesFrame.java
File metadata and controls
69 lines (55 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package mines;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 扫雷主框架类
* @author Leslie Leung
*/
public class MinesFrame extends JFrame{
private MinesFields ms; //表示扫雷游戏面板
private JButton btnStart; //表示“重新开始”按钮
/**
* 构造方法
*/
public MinesFrame(){
setSize(500, 500); //设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); //设置窗体于屏幕中央
setTitle("Mine"); //设置标题为Mine
setResizable(false); //不允许窗体缩放
setLayout(new FlowLayout()); //设置布局管理器
ms = new MinesFields(); //新建场景类对象
btnStart = new JButton("重新开始"); //新建按钮
add(btnStart); //把按钮添加到框架中
add(ms); //把扫雷场景添加到框架中
/* 注册鼠标事件 */
addMouseListener(ms.getInnerInstance());
ms.addMouseListener(ms.getInnerInstance());
/* 使用匿名内部类的方式监听按钮事件,让游戏重新开始 */
btnStart.addActionListener(new Restart());
setVisible(true);
}
/**
* 内部类,用于实现点击按钮重新开始游戏的功能
* @author Leslie Leung
*/
private class Restart implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
remove(ms); //先移除面板
ms = new MinesFields(); //再新建
add(ms); //添加新建的MinesFields对象
/* 注册鼠标事件 */
addMouseListener(ms.getInnerInstance());
ms.addMouseListener(ms.getInnerInstance());
setVisible(true);
}
}
public static void main(String[] args) {
new MinesFrame();
}
}