forked from gjy03/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGameFrame.java
More file actions
85 lines (70 loc) · 2.63 KB
/
ChessGameFrame.java
File metadata and controls
85 lines (70 loc) · 2.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package view;
import controller.GameController;
import javax.swing.*;
import java.awt.*;
/**
* 这个类表示游戏过程中的整个游戏界面,是一切的载体
*/
public class ChessGameFrame extends JFrame {
// public final Dimension FRAME_SIZE ;
private final int WIDTH;
private final int HEIGTH;
public final int CHESSBOARD_SIZE;
private GameController gameController;
public ChessGameFrame(int width, int height) {
setTitle("2022 CS102A Project Demo"); //设置标题
this.WIDTH = width;
this.HEIGTH = height;
this.CHESSBOARD_SIZE = HEIGTH * 4 / 5;
setSize(WIDTH, HEIGTH);
setLocationRelativeTo(null); // Center the window.
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置程序关闭按键,如果点击右上方的叉就游戏全部关闭了
setLayout(null);
addChessboard();
addLabel();
addHelloButton();
addLoadButton();
}
/**
* 在游戏面板中添加棋盘
*/
private void addChessboard() {
Chessboard chessboard = new Chessboard(CHESSBOARD_SIZE, CHESSBOARD_SIZE);
gameController = new GameController(chessboard);
chessboard.setLocation(HEIGTH / 10, HEIGTH / 10);
add(chessboard);
}
/**
* 在游戏面板中添加标签
*/
private void addLabel() {
JLabel statusLabel = new JLabel("Sample label");
statusLabel.setLocation(HEIGTH, HEIGTH / 10);
statusLabel.setSize(200, 60);
statusLabel.setFont(new Font("Rockwell", Font.BOLD, 20));
add(statusLabel);
}
/**
* 在游戏面板中增加一个按钮,如果按下的话就会显示Hello, world!
*/
private void addHelloButton() {
JButton button = new JButton("Show Hello Here");
button.addActionListener((e) -> JOptionPane.showMessageDialog(this, "Hello, world!"));
button.setLocation(HEIGTH, HEIGTH / 10 + 120);
button.setSize(200, 60);
button.setFont(new Font("Rockwell", Font.BOLD, 20));
add(button);
}
private void addLoadButton() {
JButton button = new JButton("Load");
button.setLocation(HEIGTH, HEIGTH / 10 + 240);
button.setSize(200, 60);
button.setFont(new Font("Rockwell", Font.BOLD, 20));
add(button);
button.addActionListener(e -> {
System.out.println("Click load");
String path = JOptionPane.showInputDialog(this,"Input Path here");
gameController.loadGameFromFile(path);
});
}
}