想想自己以前学过很多内容,但都是光理论不操作导致上机很少,一事无成,如今做了一个计算器的小程序练练手吧。花了两天的时间终于完成了。以后肯定要多做项目多实践!
下面是代码,写的不简洁,不规范。

1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 public class Calc extends MouseAdapter {
6 JFrame list;
7 // Container con;
8 JTextField show;
9 JButton[] jbNum = new JButton[10];
10 JPanel jpMain; // 主面板
11 JPanel jpRight; // 右子面板主要用于存放运算符和等号
12 JPanel jpLight; // 左子面板用于存放数字,符号, “.”
13 JButton dight; // 小数点
14 JButton sign; // 正负号
15 JButton add; // 加号
16 JButton sub; // 减号
17 JButton multiply; // 乘号
18 JButton divide; // 除号
19 JButton power; // 求幂
20 JButton cos; // cos
21 JButton sin; // sin
22 JButton ln; // ln
23 JButton ce; // 清除
24 JButton equal; // 等于
25 JButton mod; // 取余
26 JButton sqrt; // sqrt
27 double sum = 0; // 临时结果
28 boolean b = false; // 监控运算符是否被点击,错误是否出现,用于实现下一次点击按钮时清空
29 operator i = operator.un; // 记录等号符点击前某一运算符点击次数,用于实现连加或者连减等
30
31 int op; // 记录操作符
32
33 // 操作符一包括+-*/%^
34 enum operator {
35 add, sub, mul, div, mod, pow, sin, cos, sqrt, ln, un
36 }
37
38 void display() {
39 // 创建主窗口,添加一个Text框,
40 list = new JFrame("计算器");
41 list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42 list.setSize(360, 230);
43 list.setLocation(400, 300);
44 list.setBackground(Color.LIGHT_GRAY); // 设置窗口背景颜色
45 list.setResizable(false);
46
47 list.setLayout(new FlowLayout(FlowLayout.CENTER));
48 show = new JTextField(31);
49 show.setHorizontalAlignment(JTextField.RIGHT); // 文本框内文字右对齐
50 show.setEditable(false); // 文本框不可编辑
51 list.add(show);
52 // 创建面板并设置布局
53 jpMain = new JPanel();
54 jpRight = new JPanel();
55 jpLight = new JPanel();
56 jpMain.setLayout(new GridLayout(1, 2));
57 jpRight.setLayout(new GridLayout(4, 3, 3, 3));
58 jpLight.setLayout(new GridLayout(4, 3, 3, 3));
59 list.add(jpMain);
60 jpMain.add(jpLight);
61 jpMain.add(jpRight);
62 // 创建0~9按钮对象
63 for (int i = 9; i >= 0; i--) {
64 jbNum[i] = new JButton(String.valueOf(i));
65 jbNum[i].setForeground(Color.BLUE);
66 jpLight.add(jbNum[i]);
67 jbNum[i].addMouseListener(this);
68 }
69 add = new JButton("+");
70 sub = new JButton("-");
71 multiply = new JButton("*");
72 divide = new JButton("/");
73 power = new JButton("x^y");
74 sin = new JButton("sin");
75 cos = new JButton("cos");
76 ln = new JButton("ln");
77 ce = new JButton("CE");
78 equal = new JButton("=");
79 mod = new JButton("%");
80 sqrt = new JButton("sqrt");
81 jpRight.add(divide);
82 jpRight.add(sqrt);
83 jpRight.add(ln);
84 jpRight.add(multiply);
85 jpRight.add(sin);
86 jpRight.add(mod);
87 jpRight.add(sub);
88 jpRight.add(cos);
89 jpRight.add(ce);
90 jpRight.add(add);
91 jpRight.add(power);
92 jpRight.add(equal);
93
94 // 给所有按钮注册监听器
95 dight = new JButton(".");
96 sign = new JButton("±");
97 jpLight.add(sign);
98 jpLight.add(dight);
99 add.addMouseListener(this);
100 sub.addMouseListener(this);
101 multiply.addMouseListener(this);
102 divide.addMouseListener(this);
103 power.addMouseListener(this);
104 sin.addMouseListener(this);
105 cos.addMouseListener(this);
106 ln.addMouseListener(this);
107 ce.addMouseListener(this);
108 equal.addMouseListener(this);
109 mod.addMouseListener(this);
110 sqrt.addMouseListener(this);
111 dight.addMouseListener(this);
112 sign.addMouseListener(this);
113 list.setVisible(true);
114
115 }
116
117 public void mouseClicked(MouseEvent e) {
118 // 0~9的输入
119 if (e.getSource() == jbNum[0]) {
120 input(0, e);
121 }
122 if (e.getSource() == jbNum[1]) {
123 input(1, e);
124 }
125 if (e.getSource() == jbNum[2]) {
126 input(2, e);
127 }
128 if (e.getSource() == jbNum[3]) {
129 input(3, e);
130 }
131 if (e.getSource() == jbNum[4]) {
132 input(4, e);
133 }
134 if (e.getSource() == jbNum[5]) {
135 input(5, e);
136 }
137 if (e.getSource() == jbNum[6]) {
138 input(6, e);
139 }
140 if (e.getSource() == jbNum[7]) {
141 input(7, e);
142 }
143 if (e.getSource() == jbNum[8]) {
144 input(8, e);
145 }
146 if (e.getSource() == jbNum[9]) {
147 input(9, e);
148 }
149
150 // 小数点,正负号,CE,等号
151 if (e.getSource() == dight) {
152 if (show.getText().indexOf('.') == -1) {
153 show.setText(show.getText() + ".");
154 }
155
156 }
157 if (e.getSource() == sign) {
158 if (show.getText().indexOf("-") == -1) {
159 show.setText("-" + show.getText());
160 } else {
161 show.setText(show.getText().replace('-', '\0'));
162 }
163
164 }
165 if (e.getSource() == ce) {
166 show.setText("0");
167 sum = 0;
168 i = operator.un;
169 b = false;
170 }
171 outer: if (e.getSource() == equal) {
172 try {
173 if (i == operator.un) {
174 b = true;
175 } else {
176 if (i == operator.add) {
177 sum += Double.parseDouble(show.getText());
178
179 }
180 if (i == operator.sub) {
181 sum -= Double.parseDouble(show.getText());
182
183 }
184 if (i == operator.mul) {
185 sum *= Double.parseDouble(show.getText());
186
187 }
188 if (i == operator.div) {
189 if (Double.parseDouble(show.getText()) != 0) {
190 sum /= Double.parseDouble(show.getText());
191
192 } else {
193 show.setText("ERROR");
194 b = true;
195 sum = 0;
196 break outer; // 不执行trimIn()方法 屏幕显示错误
197 }
198 }
199 if (i == operator.mod) {
200 sum %= Double.parseDouble(show.getText());
201
202 }
203 if (i == operator.pow) {
204 sum = Math.pow(sum, Double.parseDouble(show.getText()));
205
206 }
207 trimIn(sum);
208 }
209 } catch (Exception ex) {
210 show.setText("ERROR");
211 b = true;
212 sum = 0;
213 }
214
215 sum = 0;
216 i = operator.un;
217 b = true;
218 }
219 // 加减乘除//幂指函数//取余
220 if (e.getSource() == add) {
221 cal(i);
222 i = operator.add;
223 b = true;
224
225 }
226 if (e.getSource() == sub) {
227 cal(i);
228 i = operator.sub;
229 b = true;
230
231 }
232 if (e.getSource() == multiply) {
233 cal(i);
234 i = operator.mul;
235 b = true;
236
237 }
238 if (e.getSource() == divide) {
239 cal(i);
240 i = operator.div;
241 b = true;
242
243 }
244 if (e.getSource() == mod) {
245 cal(i);
246 i = operator.mod;
247 b = true;
248
249 }
250 if (e.getSource() == power) {
251 cal(i);
252 i = operator.pow;
253 b = true;
254
255 }
256
257 // sqrt,sin,cos,ln
258 try {
259 if (show.getText() != "ERROR") {
260 if (e.getSource() == sqrt) {
261 sum = Math.sqrt(Double.parseDouble(show.getText()));
262 trimIn(sum);
263 b = true;
264 }
265 if (e.getSource() == sin) {
266 sum = Math.sin(Double.parseDouble(show.getText()));
267 trimIn(sum);
268 b = true;
269 }
270 if (e.getSource() == cos) {
271 sum = Math.cos(Double.parseDouble(show.getText()));
272 trimIn(sum);
273 b = true;
274 }
275 if (e.getSource() == ln) {
276 sum = Math.log(Double.parseDouble(show.getText()));
277 trimIn(sum);
278 b = true;
279 }
280 }
281 } catch (Exception ex) {
282 show.setText("ERROR");
283 b = true;
284 }
285 }
286
287 // 用以四则运算和求幂和取模的方法
288 public void cal(operator i) {
289 try {
290 if (show.getText() != "ERROR") {
291 if (i == operator.un) {
292 sum = Double.parseDouble(show.getText());
293 }
294 if (i == operator.add) {
295 sum += Double.parseDouble(show.getText());
296 trimIn(sum);
297 }
298 if (i == operator.sub) {
299 sum -= Double.parseDouble(show.getText());
300 trimIn(sum);
301 }
302 if (i == operator.mul) {
303 sum *= Double.parseDouble(show.getText());
304 trimIn(sum);
305 }
306 if (i == operator.div) {
307 if (Double.parseDouble(show.getText()) != 0) {
308 sum /= Double.parseDouble(show.getText());
309 trimIn(sum);
310 } else {
311 //出现0后,把一切数据重置
312 show.setText("ERROR");
313 sum = 0;
314 b = true;
315 i=operator.un;
316 }
317 }
318 //取余
319 if (i == operator.mod) {
320 sum %= Double.parseDouble(show.getText());
321 trimIn(sum);
322 }
323 //幂指函数
324 if (i == operator.pow) {
325 sum = Math.pow(sum, Double.parseDouble(show.getText()));
326 trimIn(sum);
327 }
328 }
329 } catch (Exception ex) {
330 show.setText("ERROR");
331 b = true;
332 }
333 }
334
335 // 点击数字输入
336 public void input(int i, MouseEvent e) {
337 if (b == true) {
338 show.setText(String.valueOf(i));
339 b = false;
340 } else {
341 //判断0和.来清除整数时后面的点
342 if (show.getText().indexOf('0') == 0 && e.getSource() != dight) {
343 show.setText(String.valueOf(i));
344 } else {
345 show.setText(show.getText() + String.valueOf(i));
346 }
347 }
348 }
349
350 // sum的显示,整数的去掉小数点和0
351 public void trimIn(double sum) {
352 // if (show.getText().indexOf('.') != -1 &&
353 // show.getText().endsWith("0")) {
354 // show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)
355 // .indexOf('.'))));
356 // } else
357 if (String.valueOf(sum).indexOf('.') != -1
358 && String.valueOf(sum).endsWith("0")) {
359 show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)
360 .indexOf('.'))));
361
362 } else if (Double.isNaN(sum)) {
363 show.setText("ERROR"); //不 是数字时 屏幕显示错误,并把sum置于0 运算符置UN
364 b = true;
365 sum = 0;
366 i = operator.un;
367 } else if (Double.isInfinite(sum)) {
368 show.setText("ERROR"); //出现infinite(无限大)时显示错误SUM置0运算符置UN
369 b = true;
370 sum = 0;
371 i = operator.un;
372 } else {
373 show.setText(String.valueOf(sum));
374 }
375 }
376
377 /**
378 * @param args
379 */
380 public static void main(String[] args) {
381 // TODO Auto-generated method stub
382 Calc c = new Calc();
383 c.display();
384 }
385
386 }