-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCalculator.java
More file actions
353 lines (306 loc) · 10.1 KB
/
MyCalculator.java
File metadata and controls
353 lines (306 loc) · 10.1 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
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyCalculator {
private JFrame frame;
private JTextField textField;
private JButton btn8;
private JButton btn9;
private JButton btn;
private JButton btnmin;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btnmul;
private JButton btn2;
private JButton btn1;
double firstnum;
double secondnum;
double result;
String operations;
String answer;
/**
* 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();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 251, 383);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("Arial", Font.BOLD, 18));
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10, 11, 215, 39);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn7.getText();
textField.setText(EnterNumber);
}
});
btn7.setFont(new Font("Arial", Font.BOLD, 20));
btn7.setBounds(10, 116, 50, 50);
frame.getContentPane().add(btn7);
btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn8.getText();
textField.setText(EnterNumber);
}
});
btn8.setFont(new Font("Arial", Font.BOLD, 20));
btn8.setBounds(65, 116, 50, 50);
frame.getContentPane().add(btn8);
btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn9.getText();
textField.setText(EnterNumber);
}
});
btn9.setFont(new Font("Arial", Font.BOLD, 20));
btn9.setBounds(120, 116, 50, 50);
frame.getContentPane().add(btn9);
btnmin = new JButton("-");
btnmin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations= "-";
}
});
btnmin.setFont(new Font("Arial", Font.BOLD, 20));
btnmin.setBounds(175, 116, 50, 50);
frame.getContentPane().add(btnmin);
btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn4.getText();
textField.setText(EnterNumber);
}
});
btn4.setFont(new Font("Arial", Font.BOLD, 20));
btn4.setBounds(10, 171, 50, 50);
frame.getContentPane().add(btn4);
btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn5.getText();
textField.setText(EnterNumber);
}
});
btn5.setFont(new Font("Arial", Font.BOLD, 20));
btn5.setBounds(65, 171, 50, 50);
frame.getContentPane().add(btn5);
btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn6.getText();
textField.setText(EnterNumber);
}
});
btn6.setFont(new Font("Arial", Font.BOLD, 20));
btn6.setBounds(120, 171, 50, 50);
frame.getContentPane().add(btn6);
btnmul = new JButton("*");
btnmul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations= "*";
}
});
btnmul.setFont(new Font("Arial", Font.BOLD, 20));
btnmul.setBounds(175, 171, 50, 50);
frame.getContentPane().add(btnmul);
btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn2.getText();
textField.setText(EnterNumber);
}
});
btn2.setFont(new Font("Arial", Font.BOLD, 20));
btn2.setBounds(65, 226, 50, 50);
frame.getContentPane().add(btn2);
btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn1.getText();
textField.setText(EnterNumber);
}
});
btn1.setFont(new Font("Arial", Font.BOLD, 20));
btn1.setBounds(10, 226, 50, 50);
frame.getContentPane().add(btn1);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn3.getText();
textField.setText(EnterNumber);
}
});
btn3.setFont(new Font("Arial", Font.BOLD, 20));
btn3.setBounds(120, 226, 50, 50);
frame.getContentPane().add(btn3);
JButton btnd = new JButton("/");
btnd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations= "/";
}
});
btnd.setFont(new Font("Arial", Font.BOLD, 20));
btnd.setHorizontalAlignment(SwingConstants.RIGHT);
btnd.setBounds(175, 226, 50, 50);
frame.getContentPane().add(btnd);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn0.getText();
textField.setText(EnterNumber);
}
});
btn0.setFont(new Font("Arial", Font.BOLD, 20));
btn0.setBounds(10, 281, 50, 50);
frame.getContentPane().add(btn0);
JButton btndot = new JButton(".");
btndot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btndot.getText();
textField.setText(EnterNumber);
}
});
btndot.setFont(new Font("Arial", Font.BOLD, 20));
btndot.setBounds(65, 281, 50, 50);
frame.getContentPane().add(btndot);
JButton btnequal = new JButton("=");
btnequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String answer;
secondnum= Double.parseDouble(textField.getText());
if(operations =="+")
{
result = firstnum+ secondnum;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operations =="-")
{
result = firstnum- secondnum;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operations =="*")
{
result = firstnum* secondnum;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operations =="/")
{
result = firstnum/ secondnum;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operations =="%")
{
result = firstnum% secondnum;
answer=String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnequal.setFont(new Font("Arial", Font.BOLD, 20));
btnequal.setBounds(175, 281, 50, 50);
frame.getContentPane().add(btnequal);
JButton btnplmi = new JButton("+-");
btnplmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double ops = Double.parseDouble(String.valueOf(textField.getText()));
ops =ops * (-1);
textField.setText(String.valueOf(ops));
}
});
btnplmi.setFont(new Font("Arial", Font.BOLD, 12));
btnplmi.setBounds(120, 281, 50, 50);
frame.getContentPane().add(btnplmi);
JButton btnback = new JButton("<-");
btnback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String btnback=null;
if(textField.getText().length()>0)
{
StringBuilder strB = new StringBuilder(textField.getText());
strB.deleteCharAt(textField.getText().length() - 1);
btnback = strB.toString();
textField.setText(btnback);
}
}
});
btnback.setFont(new Font("Arial", Font.BOLD, 12));
btnback.setBounds(10, 63, 50, 50);
frame.getContentPane().add(btnback);
JButton btnC = new JButton("C");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(null);
}
});
btnC.setFont(new Font("Arial", Font.BOLD, 20));
btnC.setBounds(65, 63, 50, 50);
frame.getContentPane().add(btnC);
JButton btnmod = new JButton("%");
btnmod.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations= "%";
}
});
btnmod.setFont(new Font("Arial", Font.BOLD, 12));
btnmod.setBounds(120, 63, 50, 50);
frame.getContentPane().add(btnmod);
JButton btnpl = new JButton("+");
btnpl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations= "+";
}
});
btnpl.setFont(new Font("Arial", Font.BOLD, 20));
btnpl.setBounds(175, 63, 50, 50);
frame.getContentPane().add(btnpl);
}
}