-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
321 lines (293 loc) · 9.22 KB
/
Main.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
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class Main {
private JFrame frame;
private JTextField textField;
double first;
double second;
double result;
String operation;
String answer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 294, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 11, 256, 65);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnBackspace = new JButton("\uF0E7");
btnBackspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String backSpace=null;
if(textField.getText().length()>0)
{
StringBuilder str=new StringBuilder(textField.getText());
str.deleteCharAt(textField.getText().length()-1);
backSpace=str.toString();
textField.setText(backSpace);
}
}
});
btnBackspace.setFont(new Font("Wingdings", Font.BOLD, 18));
btnBackspace.setBounds(10, 87, 65, 54);
frame.getContentPane().add(btnBackspace);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn7.getText();
textField.setText(number);
}
});
btn7.setFont(new Font("Tahoma", Font.BOLD, 18));
btn7.setBounds(10, 140, 65, 54);
frame.getContentPane().add(btn7);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn4.getText();
textField.setText(number);
}
});
btn4.setFont(new Font("Tahoma", Font.BOLD, 18));
btn4.setBounds(10, 191, 65, 54);
frame.getContentPane().add(btn4);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn1.getText();
textField.setText(number);
}
});
btn1.setFont(new Font("Tahoma", Font.BOLD, 18));
btn1.setBounds(10, 244, 65, 54);
frame.getContentPane().add(btn1);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn0.getText();
textField.setText(number);
}
});
btn0.setFont(new Font("Tahoma", Font.BOLD, 18));
btn0.setBounds(10, 296, 65, 54);
frame.getContentPane().add(btn0);
JButton btnClear = new JButton("CLE");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(null);
}
});
btnClear.setFont(new Font("Tahoma", Font.BOLD, 18));
btnClear.setBounds(75, 87, 65, 54);
frame.getContentPane().add(btnClear);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn8.getText();
textField.setText(number);
}
});
btn8.setFont(new Font("Tahoma", Font.BOLD, 18));
btn8.setBounds(75, 140, 65, 54);
frame.getContentPane().add(btn8);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn5.getText();
textField.setText(number);
}
});
btn5.setFont(new Font("Tahoma", Font.BOLD, 18));
btn5.setBounds(75, 191, 65, 54);
frame.getContentPane().add(btn5);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn2.getText();
textField.setText(number);
}
});
btn2.setFont(new Font("Tahoma", Font.BOLD, 18));
btn2.setBounds(75, 244, 65, 54);
frame.getContentPane().add(btn2);
JButton btnDot = new JButton(".");
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btnDot.getText();
textField.setText(number);
}
});
btnDot.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDot.setBounds(75, 296, 65, 54);
frame.getContentPane().add(btnDot);
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String answer;
second=Double.parseDouble(textField.getText());
if(operation=="+")
{
result=first+second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operation=="-")
{
result=first-second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operation=="*")
{
result=first*second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operation=="/")
{
result=first/second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if(operation=="%")
{
result=first%second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnEqual.setFont(new Font("Tahoma", Font.BOLD, 18));
btnEqual.setBounds(141, 296, 65, 54);
frame.getContentPane().add(btnEqual);
JButton btnPercent = new JButton("%");
btnPercent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="%";
}
});
btnPercent.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPercent.setBounds(206, 296, 65, 54);
frame.getContentPane().add(btnPercent);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn3.getText();
textField.setText(number);
}
});
btn3.setFont(new Font("Tahoma", Font.BOLD, 18));
btn3.setBounds(141, 244, 65, 54);
frame.getContentPane().add(btn3);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn6.getText();
textField.setText(number);
}
});
btn6.setFont(new Font("Tahoma", Font.BOLD, 18));
btn6.setBounds(141, 191, 65, 54);
frame.getContentPane().add(btn6);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn9.getText();
textField.setText(number);
}
});
btn9.setFont(new Font("Tahoma", Font.BOLD, 18));
btn9.setBounds(141, 140, 65, 54);
frame.getContentPane().add(btn9);
JButton btn00 = new JButton("00");
btn00.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String number=textField.getText()+btn00.getText();
textField.setText(number);
}
});
btn00.setFont(new Font("Tahoma", Font.BOLD, 18));
btn00.setBounds(141, 87, 65, 54);
frame.getContentPane().add(btn00);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="+";
}
});
btnPlus.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPlus.setBounds(206, 87, 65, 54);
frame.getContentPane().add(btnPlus);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="-";
}
});
btnSub.setFont(new Font("Tahoma", Font.BOLD, 18));
btnSub.setBounds(206, 140, 65, 54);
frame.getContentPane().add(btnSub);
JButton btnMul = new JButton("*");
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="*";
}
});
btnMul.setFont(new Font("Tahoma", Font.BOLD, 18));
btnMul.setBounds(206, 191, 65, 54);
frame.getContentPane().add(btnMul);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="/";
}
});
btnDivide.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDivide.setBounds(206, 244, 65, 54);
frame.getContentPane().add(btnDivide);
}
}