-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
363 lines (306 loc) · 8.63 KB
/
Calculator.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.shiyanlou.calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.math.BigDecimal;
public class Calculator {
// 操作数1,为了程序的安全,初值一定设置,这里我们设置为0。
String str1 = "0";
// 操作数2
String str2 = "0";
// 运算符
String signal = "+";
// 运算结果
String result = "";
// 以下k1至k2为状态开关
// 开关1用于选择输入方向,将要写入str1或str2
int k1 = 1;
// 开关2用于记录符号键的次数,如果 k2>1 说明进行的是 2+3-9+8 这样的多符号运算
int k2 = 1;
// 开关3用于标识 str1 是否可以被清0 ,等于1时可以,不等于1时不能被清0
int k3 = 1;
// 开关4用于标识 str2 是否可以被清0
int k4 = 1;
// 开关5用于控制小数点可否被录入,等于1时可以,不为1时,输入的小数点被丢掉
int k5 = 1;
// store的作用类似于寄存器,用于记录是否连续按下符号键
JButton store;
@SuppressWarnings("rawtypes")
Vector vt = new Vector(20, 10);
// 声明各个UI组件对象并初始化
JFrame frame = new JFrame("Calculator");
JTextField result_TextField = new JTextField(result, 20);
JButton clear_Button = new JButton("Clear");
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button_Dian = new JButton(".");
JButton button_jia = new JButton("+");
JButton button_jian = new JButton("-");
JButton button_cheng = new JButton("*");
JButton button_chu = new JButton("/");
JButton button_dy = new JButton("=");
// 计算机类的构造器
public Calculator() {
// 为按钮设置等效键,即可以通过对应的键盘按键来代替点击它
button0.setMnemonic(KeyEvent.VK_0);
// 其它等效键省略,你可以自行补充完整
// 设置文本框为右对齐,使输入和结果都靠右显示
result_TextField.setHorizontalAlignment(JTextField.RIGHT);
// 将UI组件添加进容器内
JPanel pan = new JPanel();
pan.setLayout(new GridLayout(4, 4, 5, 5));
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel pan2 = new JPanel();
pan2.setLayout(new BorderLayout());
pan2.add(result_TextField, BorderLayout.WEST);
pan2.add(clear_Button, BorderLayout.EAST);
// 设置主窗口出现在屏幕上的位置
frame.setLocation(300, 200);
// 设置窗体不能调大小
frame.setResizable(false);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2, BorderLayout.NORTH);
frame.getContentPane().add(pan, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
// 事件处理程序
// 数字键
class Listener implements ActionListener {
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
String ss = ((JButton) e.getSource()).getText();
store = (JButton) e.getSource();
vt.add(store);
if (k1 == 1) {
if (k3 == 1) {
str1 = "";
// 还原开关k5状态
k5 = 1;
}
str1 = str1 + ss;
k3 = k3 + 1;
// 显示结果
result_TextField.setText(str1);
} else if (k1 == 2) {
if (k4 == 1) {
str2 = "";
// 还原开关k5状态
k5 = 1;
}
str2 = str2 + ss;
k4 = k4 + 1;
result_TextField.setText(str2);
}
}
}
// 输入的运算符号的处理
class Listener_signal implements ActionListener {
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
String ss2 = ((JButton) e.getSource()).getText();
store = (JButton) e.getSource();
vt.add(store);
if (k2 == 1) {
// 开关 k1 为 1 时向数 1 写输入值,为2时向数2写输入值。
k1 = 2;
k5 = 1;
signal = ss2;
k2 = k2 + 1;// 按符号键的次数
} else {
int a = vt.size();
JButton c = (JButton) vt.get(a - 2);
if (!(c.getText().equals("+"))
&& !(c.getText().equals("-"))
&& !(c.getText().equals("*"))
&& !(c.getText().equals("/")))
{
cal();
str1 = result;
// 开关 k1 为 1 时,向数 1 写值,为2时向数2写
k1 = 2;
k5 = 1;
k4 = 1;
signal = ss2;
}
k2 = k2 + 1;
}
}
}
// 清除键的逻辑(Clear)
class Listener_clear implements ActionListener {
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
k5 = 1;
k2 = 1;
k1 = 1;
k3 = 1;
k4 = 1;
str1 = "0";
str2 = "0";
signal = "";
result = "";
result_TextField.setText(result);
vt.clear();
}
}
// 等于键的逻辑
class Listener_dy implements ActionListener {
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
cal();
// 还原各个开关的状态
k1 = 1;
k2 = 1;
k3 = 1;
k4 = 1;
str1 = result;
}
}
// 小数点的处理
class Listener_xiaos implements ActionListener {
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
if (k5 == 1) {
String ss2 = ((JButton) e.getSource()).getText();
if (k1 == 1) {
if (k3 == 1) {
str1 = "";
// 还原开关k5状态
k5 = 1;
}
str1 = str1 + ss2;
k3 = k3 + 1;
// 显示结果
result_TextField.setText(str1);
} else if (k1 == 2) {
if (k4 == 1) {
str2 = "";
// 还原开关k5的状态
k5 = 1;
}
str2 = str2 + ss2;
k4 = k4 + 1;
result_TextField.setText(str2);
}
}
k5 = k5 + 1;
}
}
// 注册各个监听器,即绑定事件响应逻辑到各个UI组件上
Listener_dy jt_dy = new Listener_dy();
// 监听数字键
Listener jt = new Listener();
// 监听符号键
Listener_signal jt_signal = new Listener_signal();
// 监听清除键
Listener_clear jt_c = new Listener_clear();
// 监听小数点键
Listener_xiaos jt_xs = new Listener_xiaos();
button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_signal);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_signal);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_signal);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_signal);
clear_Button.addActionListener(jt_c);
// 窗体关闭事件的响应程序
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 计算逻辑
public void cal() {
// 操作数1
double a2;
// 操作数2
double b2;
// 运算符
String c = signal;
// 运算结果
double result2 = 0;
if (c.equals("")) {
result_TextField.setText("Please input operator");
} else {
// 手动处理小数点的问题
if (str1.equals("."))
str1 = "0.0";
if (str2.equals("."))
str2 = "0.0";
a2 = Double.valueOf(str1).doubleValue();
b2 = Double.valueOf(str2).doubleValue();
if (c.equals("+")) {
result2 = a2 + b2;
}
if (c.equals("-")) {
result2 = a2 - b2;
}
if (c.equals("*")) {
BigDecimal m1 = new BigDecimal(Double.toString(a2));
BigDecimal m2 = new BigDecimal(Double.toString(b2));
result2 = m1.multiply(m2).doubleValue();
}
if (c.equals("/")) {
if (b2 == 0) {
result2 = 0;
} else {
result2 = a2 / b2;
}
}
result = ((new Double(result2)).toString());
result_TextField.setText(result);
}
}
@SuppressWarnings("unused")
public static void main(String[] args) {
// 设置程序显示的界面风格,可以去除
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
Calculator cal = new Calculator();
}
}