-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
213 lines (199 loc) · 6.27 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Flow;
public class Calculator extends Frame implements ActionListener {
private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;
private TextField t1;
private String s1 = null;
private String s2 = null;
private int c = 0;
private Calculator() {
setLayout(null);
t1 = new TextField();
t1.setLocation(20, 50);
t1.setSize(350, 50);
add(t1);
b1 = new Button("7");
b1.setLocation(20, 120);
b1.setSize(75, 75);
add(b1);
b2 = new Button("8");
b2.setLocation(115, 120);
b2.setSize(75, 75);
add(b2);
b3 = new Button("9");
b3.setLocation(205, 120);
b3.setSize(75, 75);
add(b3);
b4 = new Button("%");
b4.setLocation(295, 120);
b4.setSize(75, 75);
add(b4);
b5 = new Button("4");
b5.setLocation(20, 215);
b5.setSize(75, 75);
add(b5);
b6 = new Button("5");
b6.setLocation(115, 215);
b6.setSize(75, 75);
add(b6);
b7 = new Button("6");
b7.setLocation(205, 215);
b7.setSize(75, 75);
add(b7);
b8 = new Button("X");
b8.setLocation(295, 215);
b8.setSize(75, 75);
add(b8);
b9 = new Button("1");
b9.setLocation(20, 310);
b9.setSize(75, 75);
add(b9);
b10 = new Button("2");
b10.setLocation(115, 310);
b10.setSize(75, 75);
add(b10);
b11 = new Button("3");
b11.setLocation(205, 310);
b11.setSize(75, 75);
add(b11);
b12 = new Button("+");
b12.setLocation(295, 310);
b12.setSize(75, 75);
add(b12);
b13 = new Button("-");
b13.setLocation(20, 405);
b13.setSize(75, 75);
add(b13);
b14 = new Button("0");
b14.setLocation(115, 405);
b14.setSize(75, 75);
add(b14);
b15 = new Button("/");
b15.setLocation(205, 405);
b15.setSize(75, 75);
add(b15);
b16 = new Button("=");
b16.setLocation(295, 405);
b16.setSize(75, 75);
add(b16);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
t1.setText(t1.getText() + b1.getLabel());
}
if (e.getSource() == b2) {
t1.setText(t1.getText() + b2.getLabel());
}
if (e.getSource() == b3) {
t1.setText(t1.getText() + b3.getLabel());
}
if (e.getSource() == b5) {
t1.setText(t1.getText() + b5.getLabel());
}
if (e.getSource() == b6) {
t1.setText(t1.getText() + b6.getLabel());
}
if (e.getSource() == b7) {
t1.setText(t1.getText() + b7.getLabel());
}
if (e.getSource() == b9) {
t1.setText(t1.getText() + b9.getLabel());
}
if (e.getSource() == b10) {
t1.setText(t1.getText() + b10.getLabel());
}
if (e.getSource() == b11) {
t1.setText(t1.getText() + b11.getLabel());
}
if (e.getSource() == b14) {
t1.setText(t1.getText() + b14.getLabel());
}
if (e.getSource() == b4) {
s1 = t1.getText();
c = 1;
t1.setText("");
}
if (e.getSource() == b8) {
s1 = t1.getText();
c = 2;
t1.setText("");
}
if (e.getSource() == b12) {
s1 = t1.getText();
c = 3;
t1.setText("");
}
if (e.getSource() == b13) {
s1 = t1.getText();
c = 4;
t1.setText("");
}
if (e.getSource() == b15) {
s1 = t1.getText();
c = 5;
t1.setText("");
}
int n = 0;
if (e.getSource() == b16) {
s2 = t1.getText();
if (c == 0)
t1.setText("ERROR");
else if (c == 1) {
n = Integer.parseInt(s1) % Integer.parseInt(s2);
t1.setText(String.valueOf(n));
} else if (c == 2) {
n = Integer.parseInt(s1) * Integer.parseInt(s2);
t1.setText(String.valueOf(n));
} else if (c == 3) {
n = Integer.parseInt(s1) + Integer.parseInt(s2);
t1.setText(String.valueOf(n));
} else if (c == 4) {
n = Integer.parseInt(s1) - Integer.parseInt(s2);
t1.setText(String.valueOf(n));
} else if (c == 5) {
n = Integer.parseInt(s1) / Integer.parseInt(s2);
t1.setText(String.valueOf(n));
}
}
}
public void paint(Graphics g) {
}
public static void main(String[] args) {
Calculator b = new Calculator();
b.setVisible(true);
b.setSize(400, 500);
b.setLocation(150, 70);
b.setBackground(Color.DARK_GRAY);
b.setForeground(Color.BLACK);
Font f = new Font("Times New Roman", Font.ITALIC, 20);
b.setFont(f);
Cursor c = new Cursor(Cursor.HAND_CURSOR);
b.setCursor(c);
b.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}