计算器小案例

之前做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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

/***
* author: zsui2354
* date : 2024.5.9
*/
package Calculator;

import org.w3c.dom.Text;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class Calculator extends JFrame {




/*标签*/ JLabel Display1;
/**
* 设计按钮
*/
/*按钮*/ JButton number1,number2,number3,number4,number5,number6,number7,number8,number9,number0
, Add //加
, Subtract //减
, Multiplication //乘
, Division //除
, Delete //删除
, Clear //清除
, Submit //提交
;

/*文框*/ JTextField
real_timeText = new JTextField(5),
output = new JTextField(0);

/*容器*/ JPanel
Fixed_layout = new JPanel(), //窗口内底层容器
Text_container = new JPanel(), //北部 文本框容器
Text_container2 = new JPanel(), //南部 文本框容器
Button_container = new JPanel(), //按钮容器
ButtonLine1_container = new JPanel(), //按钮行列1
ButtonLine2_container = new JPanel(), //按钮行列2
ButtonLine3_container = new JPanel(), //按钮行列3
ButtonLine4_container = new JPanel(); //按钮行列3


// ScriptEngine engine; //脚本引擎


String[] Ssuanshi_save = new String[100];

FlowLayout fl = new FlowLayout(); //流式布局
BorderLayout Bl = new BorderLayout(); //四周布局

Calculator(){


// ScriptEngineManager manager = new ScriptEngineManager();
// engine = manager.getEngineByName("JavaScript");


Display1 = new JLabel("计算后的结果:");

number1= new JButton("1")
; number2= new JButton("2")
; number3= new JButton("3")
; number4= new JButton("4")
; number5= new JButton("5")
; number6= new JButton("6")
; number7= new JButton("7")
; number8= new JButton("8")
; number9= new JButton("9")
; number0= new JButton("0")
; Add = new JButton("+")
; Subtract = new JButton("-")
; Multiplication = new JButton("*")
; Division = new JButton("/")
; Delete = new JButton("<-")
; Clear = new JButton("C")
; Submit = new JButton("计算")
;


Font buttonFont = new Font("宋体", Font.PLAIN, 21); //批量设置按钮字体和大小
number1.setFont(buttonFont);
number2.setFont(buttonFont);
number3.setFont(buttonFont);
number4.setFont(buttonFont);
number5.setFont(buttonFont);
number6.setFont(buttonFont);
number7.setFont(buttonFont);
number8.setFont(buttonFont);
number9.setFont(buttonFont);
number0.setFont(buttonFont);
Add.setFont(buttonFont);
Subtract.setFont(buttonFont);
Multiplication.setFont(buttonFont);
Division.setFont(buttonFont);
Delete.setFont(buttonFont);
Clear.setFont(buttonFont);
Submit.setFont(buttonFont);

Font textFont = new Font("宋体", Font.PLAIN, 27);
real_timeText.setFont(textFont);
output.setFont(textFont);

number1.setPreferredSize(new Dimension(50, 50)); //批量设置按钮 尺寸
number2.setPreferredSize(new Dimension(50, 50));
number3.setPreferredSize(new Dimension(50, 50));
number4.setPreferredSize(new Dimension(50, 50));
number5.setPreferredSize(new Dimension(50, 50));
number6.setPreferredSize(new Dimension(50, 50));
number7.setPreferredSize(new Dimension(50, 50));
number8.setPreferredSize(new Dimension(50, 50));
number9.setPreferredSize(new Dimension(50, 50));
number0.setPreferredSize(new Dimension(50, 50));
Add.setPreferredSize(new Dimension(50, 50));
Subtract.setPreferredSize(new Dimension(50, 50));
Multiplication.setPreferredSize(new Dimension(50, 50));
Division.setPreferredSize(new Dimension(50, 50));
Delete.setPreferredSize(new Dimension(50, 50));
Clear.setPreferredSize(new Dimension(50, 50));


ButtonListener listener = new ButtonListener();// 批量监听按钮
number1.addActionListener(listener);
number2.addActionListener(listener);
number3.addActionListener(listener);
number4.addActionListener(listener);
number5.addActionListener(listener);
number6.addActionListener(listener);
number7.addActionListener(listener);
number8.addActionListener(listener);
number9.addActionListener(listener);
number0.addActionListener(listener);
Add.addActionListener(listener);
Subtract.addActionListener(listener);
Multiplication.addActionListener(listener);
Division.addActionListener(listener);
Delete.addActionListener(listener);
Clear.addActionListener(listener);
Submit.addActionListener(listener);



super.setLayout(Bl);
super.add(Fixed_layout); //底层四周布局
Fixed_layout.add(Text_container, BorderLayout.NORTH);

Text_container.setLayout(new BorderLayout()); //将Text_container 定为 四周布局
Text_container.add(real_timeText, BorderLayout.CENTER); //将real_timeText 添加进Text_container 中心
super.add(Text_container, BorderLayout.NORTH); // 添加到窗口的上半部分
real_timeText.setPreferredSize(new Dimension(0, 50)); //设置文本框的宽高

/**
* 由于 现代计算器的UI排版我想尽可能的贴近 ,我想用四周型布局,其实是不熟悉网格布局,先用这个凑合凑合得了 :)
*/ Button_container.setLayout(new BorderLayout()); //Button_container 定为 四周布局
super.add(Button_container, BorderLayout.CENTER); //将Button_container 放置 中心
ButtonLine1_container.setLayout(new FlowLayout()); //将按钮行列 设置 流式布局
ButtonLine2_container.setLayout(new FlowLayout());
ButtonLine3_container.setLayout(new FlowLayout());


ButtonLine1_container.add(number1);
ButtonLine1_container.add(number2);
ButtonLine1_container.add(number3);
ButtonLine1_container.add(number4);
ButtonLine1_container.add(number5);
Button_container.add(ButtonLine1_container, BorderLayout.NORTH); //将 ButtonLine1_container 放置 Button_container 北
ButtonLine2_container.add(number6);
ButtonLine2_container.add(number7);
ButtonLine2_container.add(number8);
ButtonLine2_container.add(number9);
ButtonLine2_container.add(number0);
Button_container.add(ButtonLine2_container, BorderLayout.CENTER); //将 ButtonLine2_container 放置 Button_container 中心
ButtonLine3_container.add(Clear);
ButtonLine3_container.add(Division);
ButtonLine3_container.add(Multiplication);
ButtonLine3_container.add(Subtract);
ButtonLine3_container.add(Add);
Button_container.add(ButtonLine3_container, BorderLayout.SOUTH); //将 ButtonLine3_container 放置 Button_container 南

Text_container2.setLayout(new BorderLayout()); //将Text_container2 定为 四周布局
Text_container2.add(output, BorderLayout.CENTER); //将output 添加进Text_container2 中心
//Text_container2.add(Display1, BorderLayout.NORTH); //将 Display1 添加进Text_container 北
Text_container2.add(Submit, BorderLayout.NORTH); //将计算 按钮添加至 Text_container 北,它其实将 Display1 覆盖住了
super.add(Text_container2, BorderLayout.SOUTH); // 添加到窗口的下半部分
output.setPreferredSize(new Dimension(0, 50)); //设置 计算结果 文本框的宽高

//real_timeText.setEditable(false); 我觉得这里其实可以让 用户编辑 用户也可以用键盘控制计算,而不局限于按键


output.setEditable(false); //禁止编辑计算结果
super.setTitle("王果冻的小计算器");
super.setBounds(100,100,350,353);
super.setVisible(true);
super.setResizable(false); //禁用窗口大小的随意调整


}
class ButtonListener implements ActionListener { //监听
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();


switch (command) {
case "1":
String Text = real_timeText.getText();
String newText = Text + "1";
real_timeText.setText(newText);
break;
case "2":
String Text1 = real_timeText.getText();
String newText1 = Text1 + "2";
real_timeText.setText(newText1);
break;
case "3":
String Text3 = real_timeText.getText();
String newText3 = Text3 + "3";
real_timeText.setText(newText3);
break;
case "4":
String Text4 = real_timeText.getText();
String newText4 = Text4 + "4";
real_timeText.setText(newText4);
break;
case "5":
String Text5 = real_timeText.getText();
String newText5 = Text5 + "5";
real_timeText.setText(newText5);
break;
case "6":
String Text6 = real_timeText.getText();
String newText6 = Text6 + "6";
real_timeText.setText(newText6);
break;
case "7":
String Text7 = real_timeText.getText();
String newText7 = Text7 + "7";
real_timeText.setText(newText7);
break;
case "8":
String Text8 = real_timeText.getText();
String newText8 = Text8 + "8";
real_timeText.setText(newText8);
break;
case "9":
String Text9 = real_timeText.getText();
String newText9 = Text9 + "9";
real_timeText.setText(newText9);
break;
case "0":
String Text0 = real_timeText.getText();
String newText0 = Text0 + "0";
real_timeText.setText(newText0);
break;
case "+":
String Text10 = real_timeText.getText();
String newText10 = Text10 + "+";
real_timeText.setText(newText10);
break;
case "-":
String Text11 = real_timeText.getText();
String newText11 = Text11 + "-";
real_timeText.setText(newText11);
break;
case "/":
String Text12 = real_timeText.getText();
String newText12 = Text12 + "/";
real_timeText.setText(newText12);
break;
case "*":
String Text13 = real_timeText.getText();
String newText13 = Text13 + "*";
real_timeText.setText(newText13);
break;
case "计算":
String Text14 = real_timeText.getText();
Ssuanshi_save = (new String[Text14.length()]);
for (int i=0; i<Ssuanshi_save.length; i++){
Ssuanshi_save[i] = String.valueOf(Text14.charAt(i));
}
for (int i=0; i<Ssuanshi_save.length; i++){

/***
* 试验性废弃代码
*/
// if (Ssuanshi_save[i].equals("*")){
// String str1 = "";
// for (int BL_1 = i; BL_1 > 0; BL_1--) {
// str1 = String.valueOf(Ssuanshi_save[BL_1 - 1]) + str1; // 使用 + 运算符进行字符串拼接
// }
// String str2 = "";
// for (int BL_2 = i; BL_2 > 0; BL_2--) {
// str2 = String.valueOf(Ssuanshi_save[BL_2 - 1]) + str2; // 使用 + 运算符进行字符串拼接
// }


if (Ssuanshi_save[i].equals("*")) {
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();

// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 * operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("/")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();

// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 / operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("+")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();

// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 + operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("-")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();

// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 - operand2);//计算
output.setText(out);
}

}
break;
case "C": //这里的C代表清除 是Clear的首字母
real_timeText.setText("");
output.setText("");
break;


}
}
}

/***
* 本来想用下面 Script engine 这个 calculateExpression 处理计算 ,发现用时空指针, 可能是初始化 或 环境不完整 ,而引起的 空指针异常
* 感到有点 烦躁 ,我弃用了它,将其注释
* author:王果冻
*/
// public double calculateExpression(String expression) throws ScriptException {
// return (double) engine.eval(expression);
// }

public static void main(String[] args) {
Calculator OBJ = new Calculator();
}

}