-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorGUI.java
More file actions
411 lines (375 loc) · 14.5 KB
/
CalculatorGUI.java
File metadata and controls
411 lines (375 loc) · 14.5 KB
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
package calculator;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CalculatorGUI {
public static void main(String[] args) {
Font f18 = new Font("Times New Roman", Font.BOLD, 18);
Font f28 = new Font("Times New Roman", Font.BOLD, 28);
JFrame frame = new JFrame("Calculator");
frame.setSize(435, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel oldvalue = new JLabel();
JLabel op = new JLabel();
JLabel newvalue = new JLabel();
JButton btn0 = new JButton("0");
JButton btndot = new JButton(".");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btnplus = new JButton("+");
JButton btnmin = new JButton("-");
JButton btnmult = new JButton("*");
JButton btndiv = new JButton("/");
JButton btnequal = new JButton("=");
JButton btnremove = new JButton("←");
JButton btnclear = new JButton("C");
JButton btnsign = new JButton("+/-");
oldvalue.setBounds(0, 5, 380, 25);
op.setBounds(390, 5, 15, 25);
newvalue.setBounds(0, 30, 400, 100);
btnsign.setBounds(2, 135, 100, 40);
btnclear.setBounds(105, 135, 100, 40);
btnremove.setBounds(210, 135, 100, 40);
btndiv.setBounds(315, 135, 100, 40);
btn7.setBounds(2, 180, 100, 40);
btn8.setBounds(105, 180, 100, 40);
btn9.setBounds(210, 180, 100, 40);
btnmult.setBounds(315, 180, 100, 40);
btn4.setBounds(2, 225, 100, 40);
btn5.setBounds(105, 225, 100, 40);
btn6.setBounds(210, 225, 100, 40);
btnmin.setBounds(315, 225, 100, 40);
btn1.setBounds(2, 270, 100, 40);
btn2.setBounds(105, 270, 100, 40);
btn3.setBounds(210, 270, 100, 40);
btnplus.setBounds(315, 270, 100, 40);
btn0.setBounds(2, 315, 205, 40);
btndot.setBounds(210, 315, 100, 40);
btnequal.setBounds(315, 315, 100, 40);
oldvalue.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
newvalue.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
oldvalue.setOpaque(true);
oldvalue.setBackground(Color.LIGHT_GRAY);
newvalue.setOpaque(true);
newvalue.setBackground(Color.LIGHT_GRAY);
btnplus.setBackground(Color.PINK);
btndiv.setBackground(Color.PINK);
btnclear.setBackground(Color.PINK);
btnequal.setBackground(Color.PINK);
btnmin.setBackground(Color.PINK);
btnmult.setBackground(Color.PINK);
btnremove.setBackground(Color.PINK);
btnsign.setBackground(Color.PINK);
btn0.setBackground(Color.PINK);
btn1.setBackground(Color.PINK);
btn2.setBackground(Color.PINK);
btn3.setBackground(Color.PINK);
btn4.setBackground(Color.PINK);
btn5.setBackground(Color.PINK);
btn6.setBackground(Color.PINK);
btn7.setBackground(Color.PINK);
btn8.setBackground(Color.PINK);
btn9.setBackground(Color.PINK);
btndot.setBackground(Color.PINK);
oldvalue.setForeground(Color.BLACK);
newvalue.setForeground(Color.BLACK);
btnequal.setForeground(Color.BLACK);
btnplus.setForeground(Color.BLACK);
btnmult.setForeground(Color.BLACK);
btnremove.setForeground(Color.BLACK);
btnsign.setForeground(Color.BLACK);
btndiv.setForeground(Color.BLACK);
btn0.setForeground(Color.BLACK);
btn1.setForeground(Color.BLACK);
btn2.setForeground(Color.BLACK);
btn3.setForeground(Color.BLACK);
btn4.setForeground(Color.BLACK);
btn5.setForeground(Color.BLACK);
btn6.setForeground(Color.BLACK);
btn7.setForeground(Color.BLACK);
btn8.setForeground(Color.BLACK);
btn9.setForeground(Color.BLACK);
btndot.setForeground(Color.BLACK);
btnmin.setForeground(Color.BLACK);
btnclear.setForeground(Color.BLACK);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "1");
}
});
btn0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "0");
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "2");
}
});
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "3");
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "4");
}
});
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "5");
}
});
btn6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "6");
}
});
btn7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "7");
}
});
btn8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "8");
}
});
btn9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText(newvalue.getText() + "9");
}
});
btnplus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (oldvalue.getText().isEmpty()) {
oldvalue.setText(newvalue.getText());
op.setText("+");
newvalue.setText("");
} else {
try {
double result = calculate(Double.parseDouble(oldvalue.getText()), op.getText().charAt(0), Double.parseDouble(newvalue.getText()));
oldvalue.setText(String.valueOf(result));
newvalue.setText("");
op.setText("+");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btnmin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (oldvalue.getText().isEmpty()) {
oldvalue.setText(newvalue.getText());
op.setText("-");
newvalue.setText("");
} else {
try {
double result = calculate(Double.parseDouble(oldvalue.getText()), op.getText().charAt(0), Double.parseDouble(newvalue.getText()));
oldvalue.setText(String.valueOf(result));
newvalue.setText("");
op.setText("-");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btnmult.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (oldvalue.getText().isEmpty()) {
oldvalue.setText(newvalue.getText());
op.setText("*");
newvalue.setText("");
} else {
try {
double result = calculate(Double.parseDouble(oldvalue.getText()), op.getText().charAt(0), Double.parseDouble(newvalue.getText()));
oldvalue.setText(String.valueOf(result));
newvalue.setText("");
op.setText("*");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btndiv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (oldvalue.getText().isEmpty()) {
oldvalue.setText(newvalue.getText());
op.setText("/");
newvalue.setText("");
} else {
try {
double result = calculate(Double.parseDouble(oldvalue.getText()), op.getText().charAt(0), Double.parseDouble(newvalue.getText()));
oldvalue.setText(String.valueOf(result));
newvalue.setText("");
op.setText("/");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btnequal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!oldvalue.getText().isEmpty()) {
try {
double result = calculate(Double.parseDouble(oldvalue.getText()), op.getText().charAt(0), Double.parseDouble(newvalue.getText()));
oldvalue.setText("");
newvalue.setText(String.valueOf(result));
op.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btndot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!newvalue.getText().contains(".")) {
newvalue.setText(newvalue.getText() + ".");
}
}
});
btnclear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newvalue.setText("");
oldvalue.setText("");
op.setText("");
}
});
btnremove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
newvalue.setText(newvalue.getText().substring(0, newvalue.getText().length() - 1));
} catch (Exception ex) {
// Ignore
}
if (newvalue.getText().isEmpty()) {
newvalue.setText("0");
}
}
});
btnsign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num = Double.parseDouble(newvalue.getText());
num *= -1;
newvalue.setText(String.valueOf(num));
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
oldvalue.setFont(f18);
op.setFont(f18);
newvalue.setFont(f28);
btn0.setFont(f18);
btn1.setFont(f18);
btn2.setFont(f18);
btn3.setFont(f18);
btn4.setFont(f18);
btn5.setFont(f18);
btn6.setFont(f18);
btn7.setFont(f18);
btn8.setFont(f18);
btn9.setFont(f18);
btnsign.setFont(f18);
btnclear.setFont(f18);
btnequal.setFont(f18);
btndiv.setFont(f18);
btnmin.setFont(f18);
btnremove.setFont(f18);
btnplus.setFont(f18);
btndot.setFont(f18);
frame.add(oldvalue);
frame.add(op);
frame.add(newvalue);
frame.add(btn0);
frame.add(btn1);
frame.add(btndot);
frame.add(btn2);
frame.add(btn4);
frame.add(btn3);
frame.add(btn5);
frame.add(btn6);
frame.add(btn8);
frame.add(btn9);
frame.add(btnplus);
frame.add(btnmin);
frame.add(btndiv);
frame.add(btnmult);
frame.add(btnremove);
frame.add(btnsign);
frame.add(btnequal);
frame.add(btnclear);
frame.add(btndot);
frame.add(btnequal);
frame.add(btnsign);
frame.add(btnmin);
frame.add(btn7);
frame.setVisible(true);
}
public static double calculate(double num1, char operator, double num2) {
double result = 0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
JOptionPane.showMessageDialog(null, "Cannot divide by zero");
}
break;
default:
break;
}
return result;
}
}