-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorApp.java
240 lines (212 loc) · 7.09 KB
/
CalculatorApp.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorApp implements ActionListener {
JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton, subButton, mulButton, divButton, decButton, equButton, delButton, clrButton, negButton;
JPanel panel;
Font myFont = new Font("Ink Free", Font.BOLD, 30);
double num1 = 0, num2 = 0, result = 0;
char operator;
// Constructor
CalculatorApp() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setLayout(null);
// Text field setup
textfield = new JTextField();
textfield.setBounds(50, 25, 375, 50);
textfield.setFont(myFont);
textfield.setEditable(false);
frame.add(textfield);
// Initialize function buttons
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("D");
clrButton = new JButton("C");
negButton = new JButton("(-)");
// Assign function buttons to array
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;
// Add action listeners and style to function buttons
for (int i = 0; i < 9; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}
// Number buttons setup
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
numberButtons[i].addActionListener(this);
}
// Set bounds for special buttons
negButton.setBounds(50, 412, 80, 50);
delButton.setBounds(195, 412, 80, 50);
clrButton.setBounds(340, 412, 80, 50);
negButton.setBackground(Color.YELLOW);
delButton.setBackground(Color.LIGHT_GRAY);
clrButton.setBackground(Color.RED);
// Add special buttons to frame
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
// Panel setup
panel = new JPanel();
panel.setBounds(50, 100, 370, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));
// Add buttons to panel
//row 1
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
//row 2
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
//row 3
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
//row 4
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
// Add panel to frame
equButton.setBackground(Color.green);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
CalculatorApp clac = new CalculatorApp();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==decButton)
{
textfield.setText(textfield.getText().concat("."));
}
try
{
if (e.getSource() == addButton)
{
num1 = Double.parseDouble(textfield.getText());
operator = '+';
textfield.setText("");
}
}
catch (NumberFormatException ex)
{
textfield.setText("Error");
return;
}
try
{
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '-';
textfield.setText("");
}
}
catch (NumberFormatException ex)
{
textfield.setText("Error");
return;
}
try
{
if (e.getSource() == mulButton)
{
num1 = Double.parseDouble(textfield.getText());
operator = '*';
textfield.setText("");
}
} catch (NumberFormatException ex)
{
textfield.setText("Error");
return;
}
try {
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '/';
textfield.setText("");
}
} catch (NumberFormatException ex) {
textfield.setText("Error");
return;
}
try {
if (e.getSource() == equButton)
{
num2 = Double.parseDouble(textfield.getText());
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 {
textfield.setText("Error");
return;
}
break;
}
textfield.setText(String.valueOf(result));
num1 = result;
}
} catch (NumberFormatException ex) {
textfield.setText("Error");
}
if (e.getSource() == clrButton) {
textfield.setText("");
}
if(e.getSource()==delButton)
{
String string=textfield.getText();
textfield.setText("");
for(int i=0;i<string.length()-1;i++)
{
textfield.setText(textfield.getText()+string.charAt(i));
}
}
if (e.getSource() == negButton) {
double temp = Double.parseDouble(textfield.getText());
temp *= -1;
textfield.setText(String.valueOf(temp));
}
}
}