-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
274 lines (248 loc) · 8.03 KB
/
Calc.java
File metadata and controls
274 lines (248 loc) · 8.03 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
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.SystemColor;
public class Calc {
private JFrame frmCalculator;
private JTextField textField;
double fnum, snum;
double result;
String oper,answer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calc window = new Calc();
window.frmCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calc() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCalculator = new JFrame();
frmCalculator.getContentPane().setForeground(SystemColor.desktop);
frmCalculator.setTitle("Calculator");
frmCalculator.setBounds(100, 100, 261, 377);
frmCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCalculator.getContentPane().setLayout(null);
textField = new JTextField();
textField.setForeground(Color.BLACK);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10, 11, 225, 75);
frmCalculator.getContentPane().add(textField);
textField.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn7.getText();
textField.setText(EnterNumber);
}
});
btn7.setBounds(10, 97, 50, 35);
frmCalculator.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn8.getText();
textField.setText(EnterNumber);
}
});
btn8.setBounds(70, 97, 50, 35);
frmCalculator.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn9.getText();
textField.setText(EnterNumber);
}
});
btn9.setBounds(130, 97, 50, 35);
frmCalculator.getContentPane().add(btn9);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn4.getText();
textField.setText(EnterNumber);
}
});
btn4.setBounds(10, 143, 50, 35);
frmCalculator.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn5.getText();
textField.setText(EnterNumber);
}
});
btn5.setBounds(70, 143, 50, 35);
frmCalculator.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn6.getText();
textField.setText(EnterNumber);
}
});
btn6.setBounds(130, 143, 50, 35);
frmCalculator.getContentPane().add(btn6);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn1.getText();
textField.setText(EnterNumber);
}
});
btn1.setBounds(10, 189, 50, 35);
frmCalculator.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn2.getText();
textField.setText(EnterNumber);
}
});
btn2.setBounds(70, 189, 50, 35);
frmCalculator.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn3.getText();
textField.setText(EnterNumber);
}
});
btn3.setBounds(130, 189, 50, 35);
frmCalculator.getContentPane().add(btn3);
JButton btnPoint = new JButton(".");
btnPoint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btnPoint.getText();
textField.setText(EnterNumber);
}
});
btnPoint.setBounds(10, 235, 50, 35);
frmCalculator.getContentPane().add(btnPoint);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = textField.getText() +btn0.getText();
textField.setText(EnterNumber);
}
});
btn0.setBounds(70, 235, 50, 35);
frmCalculator.getContentPane().add(btn0);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnClear.setBounds(190, 97, 50, 35);
frmCalculator.getContentPane().add(btnClear);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnum = Double.parseDouble(textField.getText());
textField.setText("");
oper = "/";
}
});
btnDiv.setBounds(190, 143, 50, 35);
frmCalculator.getContentPane().add(btnDiv);
JButton btnMull = new JButton("*");
btnMull.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnum = Double.parseDouble(textField.getText());
textField.setText("");
oper = "*";
}
});
btnMull.setBounds(190, 189, 50, 35);
frmCalculator.getContentPane().add(btnMull);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnum = Double.parseDouble(textField.getText());
textField.setText("");
oper = "-";
}
});
btnSub.setBounds(130, 235, 50, 35);
frmCalculator.getContentPane().add(btnSub);
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnum = Double.parseDouble(textField.getText());
textField.setText("");
oper = "+";
}
});
btnAdd.setBounds(190, 235, 50, 35);
frmCalculator.getContentPane().add(btnAdd);
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String answer;
snum = Double.parseDouble(textField.getText());
switch(oper)
{
case "+":
fnum = fnum +snum;
answer = String.format("%.2f", fnum);
textField.setText(answer);
break;
case "-":
fnum = fnum - snum;
answer = String.format("%.2f", fnum);
textField.setText(answer);
break;
case "*":
fnum = fnum * snum;
answer = String.format("%.2f", fnum);
textField.setText(answer);
break;
case "/":
fnum = fnum / snum;
answer = String.format("%.2f", fnum);
textField.setText(answer);
break;
}
}
});
btnEquals.setBounds(146, 281, 89, 23);
frmCalculator.getContentPane().add(btnEquals);
JButton btnBack = new JButton("Delete");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Bksp = null;
if(textField.getText().length()>0)
{
StringBuilder str= new StringBuilder(textField.getText());
str.deleteCharAt(textField.getText().length() -1);
Bksp = str.toString();
textField.setText(Bksp);
}
}
});
btnBack.setBounds(10, 281, 89, 23);
frmCalculator.getContentPane().add(btnBack);
}
}