随笔 - Java: 基于 ScriptEngine 和 Swing 的极简科学计算器

基于 Java 8 编写

因为懒得写 parser, 所以直接用 javax.script.ScriptEngine#eval() 来计算表达式了,最终的代码很短

Nashorn JavaScript Engine 已于 Java 15 移除,于 Java 11 废弃 1

界面

代码

Show code

Main.javaview raw
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
import java.awt.*;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;

public class Main {

private static final Pattern patternFunc = Pattern.compile("(?<=[^a-zA-Z])([a-z]{3,})(?=[^a-zA-Z])");

public static void main(String[] args) {
AtomicReference<Double> memoryVal = new AtomicReference<>((double) 0);
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");

JFrame frame = new JFrame("Calculator");
Container container = frame.getContentPane();
container.setLayout(null);

JPanel panelTextField = new JPanel(new FlowLayout());
panelTextField.setBounds(0, 0, 600, 30);
JTextField textField = new JTextField(52);
panelTextField.add(textField);
container.add(panelTextField);

JPanel panelButtonMemory = new JPanel(new GridLayout(1, 5));
panelButtonMemory.setBounds(0, 30, 600, 50);
Buttons.buttonMapMemory
.get("MC")
.addActionListener(e -> {
memoryVal.set(.0);
});
Buttons.buttonMapMemory
.get("MR")
.addActionListener(e -> {
textField.setText(String.valueOf(memoryVal.get()));
});
Buttons.buttonMapMemory
.get("M+")
.addActionListener(e -> {
memoryVal.updateAndGet(v -> v + Double.parseDouble(textField.getText()));
});
Buttons.buttonMapMemory
.get("M-")
.addActionListener(e -> {
memoryVal.updateAndGet(v -> v - Double.parseDouble(textField.getText()));
});
Buttons.buttonMapMemory
.get("MS")
.addActionListener(e -> {
memoryVal.set(Double.parseDouble(textField.getText()));
});
for (String text : Buttons.OPERAND_MEMORY) panelButtonMemory.add(Buttons.buttonMapMemory.get(text));
container.add(panelButtonMemory);

JPanel panelButtonAdvanced = new JPanel(new GridLayout(4, 5));
panelButtonAdvanced.setBounds(0, 100, 600, 300);
for (String text : Buttons.OPERAND_ADVANCED_FRONT) {
Buttons.buttonMapAdvancedFront
.get(text)
.addActionListener(e -> {
textField.setText(text + "(" + textField.getText() + ")");
});
}
for (String text : Buttons.OPERAND_ADVANCED_BACK) {
Buttons.buttonMapAdvancedBack
.get(text)
.addActionListener(e -> {
textField.setText(textField.getText() + text);
});
}
for (String text : Buttons.OPERAND_ADVANCED_BACK) panelButtonAdvanced.add(Buttons.buttonMapAdvancedBack.get(text));
for (String text : Buttons.OPERAND_ADVANCED_FRONT) panelButtonAdvanced.add(Buttons.buttonMapAdvancedFront.get(text));
container.add(panelButtonAdvanced);

JPanel panelButtonBasic = new JPanel(new GridLayout(4, 4));
panelButtonBasic.setBounds(0, 400, 600, 200);
for (String text : Buttons.OPERAND_BASIC) {
Buttons.buttonMapBasic
.get(text)
.addActionListener(
text.equals("=")
? e -> {
try {
String queryText = patternFunc.matcher(" " + proceedPow(textField.getText()) + " ").replaceAll("Math.$1").trim();
System.out.println("Calculating: " + queryText);
textField.setText(engine.eval(queryText).toString());
System.out.println("Finished.");
} catch (ScriptException ex) {
textField.setText(ex.getMessage());
throw new RuntimeException(ex);
}
}
: e -> {
textField.setText(textField.getText() + text);
}
);
}
for (String text : Buttons.OPERAND_BASIC) panelButtonBasic.add(Buttons.buttonMapBasic.get(text));
container.add(panelButtonBasic);

frame.setSize(606, 635);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static String proceedPow(String str) {
int idx = str.indexOf('^');
while (idx != -1) {
int l = idx - 1, r = idx + 1;
int cnt = 0;
boolean f = false;
while (l > 0) {
char nowChar = str.charAt(l);
--l;
if (nowChar == ')') {
++cnt;
if (!f) f = true;
continue;
}
if (nowChar == '(') {
--cnt;
if (f && cnt == 0) break;
continue;
}
if (Character.isDigit(nowChar) || Character.isAlphabetic(nowChar)) continue;
if (cnt == 0) {
++l;
break;
}
}
cnt = 0;
while (r < str.length() - 1) {
char nowChar = str.charAt(r);
++r;
if (nowChar == '(') {
++cnt;
if (!f) f = true;
continue;
}
if (nowChar == ')') {
--cnt;
if (f && cnt == 0) break;
continue;
}
if (Character.isDigit(nowChar) || Character.isAlphabetic(nowChar)) continue;
if (cnt == 0) {
--r;
break;
}
}
str = str.substring(0, l + 1) + "pow(" + str.substring(l + 1, idx) + "," + str.substring(idx + 1, r) + ")" + str.substring(r);
idx = str.indexOf('^');
}
return str;
}
}

final class Buttons {

public static final String[] OPERAND_MEMORY = { "MC", "MR", "M+", "M-", "MS" };
public static final String[] OPERAND_ADVANCED_FRONT = { "1/", "sqrt", "round", "ceil", "floor", "abs", "exp", "2^", "10^", "log", "sin", "cos", "tan", "asin", "acos", "atan" };
public static final String[] OPERAND_ADVANCED_BACK = { "^", "(", ")", "%" };
public static final String[] OPERAND_BASIC = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", ".", "0", "=", "/" };

public static HashMap<String, JButton> buttonMapMemory = new HashMap<>();
public static HashMap<String, JButton> buttonMapAdvancedFront = new HashMap<>();
public static HashMap<String, JButton> buttonMapAdvancedBack = new HashMap<>();
public static HashMap<String, JButton> buttonMapBasic = new HashMap<>();

static {
for (String text : OPERAND_MEMORY) buttonMapMemory.put(text, new JButton(text));
for (String text : OPERAND_ADVANCED_BACK) buttonMapAdvancedBack.put(text, new JButton(text));
for (String text : OPERAND_ADVANCED_FRONT) buttonMapAdvancedFront.put(text, new JButton(text));
for (String text : OPERAND_BASIC) buttonMapBasic.put(text, new JButton(text));
}
}

  1. JDK-8236933↩︎