-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetting.java
More file actions
139 lines (134 loc) · 5.19 KB
/
Setting.java
File metadata and controls
139 lines (134 loc) · 5.19 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Setting extends JDialog implements ActionListener {
private BombTable table;
private Box baseBox, rowBox, colBox, bombBox, buttonBox;
private JTextField rowField, colField, bombField;
private JButton okButton, cancelButton;
private LimitedDocument rowDocument, colDocument, bombDocument;
private Setting(JFrame frame) {
super(frame, true);
this.setLocation(frame.getLocation().x + 100, frame.getLocation().y + 100);
setTitle("设置");
rowDocument = new LimitedDocument(8); //参数为能输入的最大长度
rowDocument.setAllowChar("0123456789"); //只能输入的字符
colDocument = new LimitedDocument(8);
colDocument.setAllowChar("0123456789");
bombDocument = new LimitedDocument(8);
bombDocument.setAllowChar("0123456789");
baseBox = Box.createVerticalBox();
rowBox = Box.createHorizontalBox();
colBox = Box.createHorizontalBox();
bombBox = Box.createHorizontalBox();
buttonBox = Box.createHorizontalBox();
rowField = new JTextField(10);
rowField.setDocument(rowDocument);
colField = new JTextField(10);
colField.setDocument(colDocument);
bombField = new JTextField(10);
bombField.setDocument(bombDocument);
okButton = new JButton("确定");
cancelButton = new JButton("取消");
baseBox.add(Box.createVerticalStrut(7));
baseBox.add(rowBox);
baseBox.add(Box.createVerticalStrut(3));
baseBox.add(colBox);
baseBox.add(Box.createVerticalStrut(3));
baseBox.add(bombBox);
baseBox.add(Box.createVerticalStrut(3));
baseBox.add(buttonBox);
rowBox.add(new JLabel("行数:"));
rowBox.add(rowField);
colBox.add(new JLabel("列数:"));
colBox.add(colField);
bombBox.add(new JLabel("雷数:"));
bombBox.add(bombField);
buttonBox.add(okButton);
buttonBox.add(Box.createHorizontalStrut(10));
buttonBox.add(cancelButton);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(baseBox);
validate();
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
okButton.addActionListener(this);
cancelButton.addActionListener(this);
}
public Setting(BombTable table) {
this((JFrame)table);
this.table = table;
}
public Setting(BombTable table, int row, int col, int bomb) {
this(table);
this.rowField.setText(String.valueOf(row));
this.colField.setText(String.valueOf(col));
this.bombField.setText(String.valueOf(bomb));
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (!check()) return;
int rowNum = Integer.parseInt(rowField.getText());
int colNum = Integer.parseInt(colField.getText());
int bombNum = Integer.parseInt(bombField.getText());
table.setTable(colNum, rowNum, bombNum);
table.validate();
table.pack();
this.dispose();
} else if (e.getSource() == cancelButton) {
this.dispose();
}
}
private boolean check() {
int rowNum;
int colNum;
int bombNum;
try {
rowNum = Integer.parseInt(rowField.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "行数格式错误",
"数据错误", JOptionPane.WARNING_MESSAGE);
rowField.requestFocus();
return false;
}
try {
colNum = Integer.parseInt(colField.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "列数格式错误",
"数据错误", JOptionPane.WARNING_MESSAGE);
colField.requestFocus();
return false;
}
try {
bombNum = Integer.parseInt(bombField.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "雷数格式错误",
"数据错误", JOptionPane.WARNING_MESSAGE);
bombField.requestFocus();
return false;
}
if (rowNum == 0) {
JOptionPane.showMessageDialog(this, "行数不能为零",
"数据错误", JOptionPane.WARNING_MESSAGE);
rowField.requestFocus();
return false;
} else if (colNum == 0) {
JOptionPane.showMessageDialog(this, "列数不能为零",
"数据错误", JOptionPane.WARNING_MESSAGE);
colField.requestFocus();
return false;
} else if (bombNum ==0) {
JOptionPane.showMessageDialog(this, "雷数不能为零",
"数据错误", JOptionPane.WARNING_MESSAGE);
bombField.requestFocus();
return false;
} else if (bombNum >= rowNum * colNum) {
JOptionPane.showMessageDialog(this, "雷数过多",
"数据错误", JOptionPane.WARNING_MESSAGE);
bombField.requestFocus();
return false;
}
return true;
}
}