-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
387 lines (360 loc) · 10.8 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
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
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import java.awt.Color;
public class Calculator {
private JFrame frame;
private JTextField txt1;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton b10;
private JButton b11;
private JButton bt1;
private JButton bt2;
private JButton bt3;
private JButton bt4;
private JButton bt5;
private JButton bt6;
double firstnum;
double secondnum;
double result;
String operation;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
public void disable()
{
txt1.setEnabled(false);
bt1.setEnabled(false);
bt2.setEnabled(false);
bt3.setEnabled(false);
bt4.setEnabled(false);
bt5.setEnabled(false);
bt6.setEnabled(false);
b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b5.setEnabled(false);
b6.setEnabled(false);
b7.setEnabled(false);
b8.setEnabled(false);
b9.setEnabled(false);
b10.setEnabled(false);
b11.setEnabled(false);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 427, 444);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txt1 = new JTextField();
txt1.setForeground(Color.BLACK);
txt1.setBackground(Color.WHITE);
txt1.setBounds(10, 11, 391, 53);
frame.getContentPane().add(txt1);
txt1.setColumns(10);
JButton bt1 = new JButton("C");
bt1.setBackground(Color.LIGHT_GRAY);
bt1.setForeground(Color.RED);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt1.setText("");
}
});
bt1.setFont(new Font("Tahoma", Font.BOLD, 20));
bt1.setBounds(180, 75, 72, 42);
frame.getContentPane().add(bt1);
JButton bt2 = new JButton("+");
bt2.setBackground(Color.LIGHT_GRAY);
bt2.setForeground(Color.RED);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum=Double.parseDouble(txt1.getText());
txt1.setText("");
operation = "+";
}
});
bt2.setFont(new Font("Tahoma", Font.BOLD, 20));
bt2.setBounds(298, 75, 72, 42);
frame.getContentPane().add(bt2);
b1 = new JButton("7");
b1.setBackground(Color.LIGHT_GRAY);
b1.setForeground(Color.RED);
b1.setFont(new Font("Tahoma", Font.BOLD, 20));
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String takein;
takein = txt1.getText() + b1.getText();
txt1.setText(takein);
}
});
b1.setBounds(10, 145, 72, 42);
frame.getContentPane().add(b1);
JButton b2 = new JButton("8");
b2.setBackground(Color.LIGHT_GRAY);
b2.setForeground(Color.RED);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b2.getText();
txt1.setText(takein);
}
});
b2.setFont(new Font("Tahoma", Font.BOLD, 20));
b2.setBounds(111, 145, 72, 42);
frame.getContentPane().add(b2);
JButton b3 = new JButton("9");
b3.setBackground(Color.LIGHT_GRAY);
b3.setForeground(Color.RED);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b1.getText();
txt1.setText(takein);
}
});
b3.setFont(new Font("Tahoma", Font.BOLD, 20));
b3.setBounds(216, 145, 72, 42);
frame.getContentPane().add(b3);
JButton b4 = new JButton("4");
b4.setBackground(Color.LIGHT_GRAY);
b4.setForeground(Color.RED);
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b4.getText();
txt1.setText(takein);
}
});
b4.setFont(new Font("Tahoma", Font.BOLD, 20));
b4.setBounds(10, 211, 72, 42);
frame.getContentPane().add(b4);
JButton b5 = new JButton("5");
b5.setBackground(Color.LIGHT_GRAY);
b5.setForeground(Color.RED);
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b5.getText();
txt1.setText(takein);
}
});
b5.setFont(new Font("Tahoma", Font.BOLD, 20));
b5.setBounds(111, 211, 72, 42);
frame.getContentPane().add(b5);
JButton b6 = new JButton("6");
b6.setBackground(Color.LIGHT_GRAY);
b6.setForeground(Color.RED);
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b6.getText();
txt1.setText(takein);
}
});
b6.setFont(new Font("Tahoma", Font.BOLD, 20));
b6.setBounds(216, 211, 72, 42);
frame.getContentPane().add(b6);
JButton b7 = new JButton("1");
b7.setBackground(Color.LIGHT_GRAY);
b7.setForeground(Color.RED);
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b7.getText();
txt1.setText(takein);
}
});
b7.setFont(new Font("Tahoma", Font.BOLD, 20));
b7.setBounds(10, 278, 72, 42);
frame.getContentPane().add(b7);
JButton b8 = new JButton("2");
b8.setBackground(Color.LIGHT_GRAY);
b8.setForeground(Color.RED);
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b8.getText();
txt1.setText(takein);
}
});
b8.setFont(new Font("Tahoma", Font.BOLD, 20));
b8.setBounds(111, 278, 72, 42);
frame.getContentPane().add(b8);
JButton b9 = new JButton("3");
b9.setBackground(Color.LIGHT_GRAY);
b9.setForeground(Color.RED);
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b9.getText();
txt1.setText(takein);
}
});
b9.setFont(new Font("Tahoma", Font.BOLD, 20));
b9.setBounds(216, 278, 72, 42);
frame.getContentPane().add(b9);
JButton b10 = new JButton("0");
b10.setBackground(Color.LIGHT_GRAY);
b10.setForeground(Color.RED);
b10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b10.getText();
txt1.setText(takein);
}
});
b10.setFont(new Font("Tahoma", Font.BOLD, 20));
b10.setBounds(10, 340, 72, 42);
frame.getContentPane().add(b10);
JButton b11 = new JButton(".");
b11.setBackground(Color.LIGHT_GRAY);
b11.setForeground(Color.RED);
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String takein;
takein = txt1.getText() + b11.getText();
txt1.setText(takein);
}
});
b11.setFont(new Font("Tahoma", Font.BOLD, 25));
b11.setBounds(111, 340, 72, 42);
frame.getContentPane().add(b11);
JButton bt6 = new JButton("=");
bt6.setBackground(Color.LIGHT_GRAY);
bt6.setForeground(Color.RED);
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String answer;
secondnum=Double.parseDouble(txt1.getText());
switch(operation)
{
case "+":
result = firstnum+secondnum;
answer = String.format("%.0f", result);
txt1.setText(answer);
break;
case "-":
result = firstnum-secondnum;
answer = String.format("%.0f", result);
txt1.setText(answer);
break;
case "*":
result = firstnum*secondnum;
answer = String.format("%.0f", result);
txt1.setText(answer);
break;
case "/":
result = firstnum/secondnum;
txt1.setText(result + "");
break;
}
}
});
bt6.setFont(new Font("Tahoma", Font.BOLD, 20));
bt6.setBounds(216, 340, 185, 42);
frame.getContentPane().add(bt6);
JButton bt3 = new JButton("-");
bt3.setBackground(Color.LIGHT_GRAY);
bt3.setForeground(Color.RED);
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum=Double.parseDouble(txt1.getText());
txt1.setText("");
operation = "-";
}
});
bt3.setFont(new Font("Tahoma", Font.BOLD, 30));
bt3.setBounds(315, 145, 72, 42);
frame.getContentPane().add(bt3);
JButton bt4 = new JButton("*");
bt4.setBackground(Color.LIGHT_GRAY);
bt4.setForeground(Color.RED);
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum=Double.parseDouble(txt1.getText());
txt1.setText("");
operation = "*";
}
});
bt4.setFont(new Font("Tahoma", Font.BOLD, 27));
bt4.setBounds(315, 211, 72, 42);
frame.getContentPane().add(bt4);
JButton bt5 = new JButton("/");
bt5.setBackground(Color.LIGHT_GRAY);
bt5.setForeground(Color.RED);
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum=Double.parseDouble(txt1.getText());
txt1.setText("");
operation = "/";
}
});
bt5.setFont(new Font("Tahoma", Font.BOLD, 20));
bt5.setBounds(315, 278, 72, 42);
frame.getContentPane().add(bt5);
JRadioButton rb1 = new JRadioButton("ON");
rb1.setBackground(Color.BLACK);
rb1.setForeground(Color.RED);
rb1.setFont(new Font("Tahoma", Font.BOLD, 15));
rb1.setBounds(10, 84, 58, 23);
frame.getContentPane().add(rb1);
JRadioButton rb2 = new JRadioButton("OFF");
rb2.setBackground(Color.BLACK);
rb2.setForeground(Color.RED);
rb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
disable();
}
});
rb2.setFont(new Font("Tahoma", Font.BOLD, 15));
rb2.setBounds(10, 110, 58, 23);
frame.getContentPane().add(rb2);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
frame.getContentPane().add(rb1);
frame.getContentPane().add(rb2);
JLabel lblCasio = new JLabel("CASIO");
lblCasio.setForeground(Color.BLACK);
lblCasio.setFont(new Font("Tempus Sans ITC", Font.BOLD | Font.ITALIC, 26));
lblCasio.setBounds(74, 87, 96, 42);
frame.getContentPane().add(lblCasio);
}
}