Fork me on GitHub

制作一个简易计算器


使用java的AWT包实现一个简易的计算器。

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
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private Frame frame;
private Label label;
private Panel panel;
private Button button0,button1,button2,button3,button4,button5,button6,button7,button8,
button9,buttonequ,buttondiv,buttondot,buttonmul,buttonsub,buttonadd;
private ButtonHandler listener;
public Calculator() {
frame = new Frame("My Calculator");
label = new Label("0", Label.RIGHT);
panel = new Panel(new GridLayout(4, 4));
button0 = new Button("0");
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
button4 = new Button("4");
button5 = new Button("5");
button6 = new Button("6");
button7 = new Button("7");
button8 = new Button("8");
button9 = new Button("9");
buttonequ = new Button("=");
buttondiv = new Button("/");
buttondot = new Button(".");
buttonmul = new Button("*");
buttonsub = new Button("-");
buttonadd = new Button("+");
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonadd);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonsub);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonmul);
panel.add(button0);
panel.add(buttondot);
panel.add(buttonequ);
panel.add(buttondiv);
frame.add(label, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
listener = new ButtonHandler();
//对按钮添加监听
button0.addActionListener(listener);
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
button4.addActionListener(listener);
button5.addActionListener(listener);
button6.addActionListener(listener);
button7.addActionListener(listener);
button8.addActionListener(listener);
button9.addActionListener(listener);
buttonadd.addActionListener(listener);
buttonsub.addActionListener(listener);
buttonmul.addActionListener(listener);
buttondiv.addActionListener(listener);
buttondot.addActionListener(listener);
buttonequ.addActionListener(listener);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class ButtonHandler implements ActionListener {
int flag = 0; //存储运算符
boolean equHappened = true;
double arg1 = 0, arg2 = 0, result = 0;
//@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonequ) {
equHappened = true;
if(flag==0) {
result = arg1 + arg2;
}
else if(flag==1) {
result = arg1 - arg2;
}
else if(flag==2) {
result = arg1 * arg2;
}
else {
result = arg1 / arg2;
}
label.setText(result+"");
}
else {
if(equHappened == true) {
label.setText("");
equHappened = false;
}
if(e.getSource() == buttondot) {
label.setText("0.");
}
else if(e.getSource() == button0) {
label.setText(label.getText()+"0");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button1) {
label.setText(label.getText()+"1");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button2) {
label.setText(label.getText()+"2");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button3) {
label.setText(label.getText()+"3");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button4) {
label.setText(label.getText()+"4");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button5) {
label.setText(label.getText()+"5");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button6) {
label.setText(label.getText()+"6");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button7) {
label.setText(label.getText()+"7");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button8) {
label.setText(label.getText()+"8");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == button9) {
label.setText(label.getText()+"9");
arg2 = Double.parseDouble(label.getText());
}
else if(e.getSource() == buttonadd) {
label.setText("");
arg1 = arg2;
flag = 0;
}
else if(e.getSource() == buttonsub) {
label.setText("");
arg1 = arg2;
flag = 1;
}
else if(e.getSource() == buttonmul) {
label.setText("");
arg1 = arg2;
flag = 2;
}
else if(e.getSource() == buttondiv) {
label.setText("");
arg1 = arg2;
flag = 3;
}
}
}
}
public static void main(String[] args) {
Calculator cal = new Calculator();
}
}

并不难,只用到了AWT,一个小时做完,感兴趣的可以用swing改写一下。

donate the author