-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.java
254 lines (199 loc) · 9.66 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
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
class Calculator implements ActionListener{
/*Global variable declaration*/
boolean isOperatorClicked =false; // boolean value for clicking a button
String oldValue;
String newValue;
String operator;
JFrame jf;
JLabel displayLabel;
JButton sevenButton;
JButton eightButton;
JButton nineButton;
JButton fourButton;
JButton fiveButton;
JButton sixButton;
JButton oneButton;
JButton twoButton;
JButton threeButton;
JButton zeroButton;
JButton dotButton;
JButton equalButton;
JButton divButton;
JButton mullButton;
JButton subButton;
JButton addButton;
JButton clearButton;
@Override
public void actionPerformed(ActionEvent e) {
String buttonText= ((JButton) e.getSource()).getText(); //buttonText will contain the text label of the button clicked.
((JButton)e.getSource()).setBackground(Color.gray); //on clicking a button its backgound changes to grey colour
if (buttonText.matches("[0-9]"))
{
if (isOperatorClicked) {
displayLabel.setText(buttonText);
isOperatorClicked = false;
} else {
displayLabel.setText(displayLabel.getText() + buttonText);
}
}
else if (buttonText.equals("."))
{
if (!displayLabel.getText().contains(".")) {
displayLabel.setText(displayLabel.getText() + buttonText);
}
}
else if (buttonText.equals("CLEAR")) {
displayLabel.setText("");
}
else if (buttonText.equals("=")) {
float newValue = Float.parseFloat(displayLabel.getText());
switch (operator) {
case "+":
displayLabel.setText(Float.toString(Float.parseFloat(oldValue) + newValue));
break;
case "-":
displayLabel.setText(Float.toString(Float.parseFloat(oldValue) - newValue));
break;
case "x":
displayLabel.setText(Float.toString(Float.parseFloat(oldValue) * newValue));
break;
case "/":
displayLabel.setText(Float.toString(Float.parseFloat(oldValue) / newValue));
break;
}
} else
{
isOperatorClicked = true;
operator = buttonText;
oldValue = displayLabel.getText();
}
}
public Calculator(){
jf=new JFrame("Calculator");
jf.setLayout(null);
jf.setSize(600, 600); // frame size
jf.setLocation(150, 150); //frame display location on screen
displayLabel=new JLabel();
displayLabel.setBounds(30, 40, 540, 40 );
displayLabel.setBackground(Color.gray);
displayLabel.setOpaque(true);
displayLabel.setHorizontalAlignment(SwingConstants.RIGHT);
displayLabel.setForeground(Color.white);
jf.add(displayLabel);
sevenButton=new JButton("7"); //seven button
sevenButton.setBounds(30, 130, 80, 80);
sevenButton.addActionListener(this);
sevenButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 7
sevenButton.setBackground(Color.gray);
jf.add(sevenButton);
eightButton=new JButton("8"); //eight button
eightButton.setBounds(130, 130, 80, 80);
eightButton.addActionListener(this);
eightButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 8
eightButton.setBackground(Color.gray);
jf.add(eightButton);
nineButton=new JButton("9"); //nine button
nineButton.setBounds(230, 130, 80, 80);
nineButton.addActionListener(this);
nineButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 9
nineButton.setBackground(Color.gray);
jf.add(nineButton);
fourButton=new JButton("4"); //four button
fourButton.setBounds(30, 230, 80, 80);
fourButton.addActionListener(this);
fourButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 4
fourButton.setBackground(Color.gray);
jf.add(fourButton);
fiveButton=new JButton("5"); //five button
fiveButton.setBounds(130, 230, 80, 80);
fiveButton.addActionListener(this);
fiveButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 5
fiveButton.setBackground(Color.gray);
jf.add(fiveButton);
sixButton=new JButton("6"); //six button
sixButton.setBounds(230, 230, 80, 80);
sixButton.addActionListener(this);
sixButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 6
sixButton.setBackground(Color.gray);
jf.add(sixButton);
oneButton=new JButton("1"); //one button
oneButton.setBounds(30, 330, 80, 80);
oneButton.addActionListener(this);
oneButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 1
oneButton.setBackground(Color.gray);
jf.add(oneButton);
twoButton=new JButton("2"); //two button
twoButton.setBounds(130, 330, 80, 80);
twoButton.addActionListener(this);
twoButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 2
twoButton.setBackground(Color.gray);
jf.add( twoButton);
threeButton=new JButton("3"); //three button
threeButton.setBounds(230, 330, 80, 80);
threeButton.addActionListener(this);
threeButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 3
threeButton.setBackground(Color.gray);
jf.add(threeButton);
dotButton=new JButton("."); //dot button
dotButton.setBounds(30, 430, 80, 80);
dotButton.addActionListener(this);
dotButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for .
dotButton.setBackground(Color.gray);
jf.add(dotButton);
zeroButton=new JButton("0"); //zero button
zeroButton.setBounds(130, 430, 80, 80);
zeroButton.addActionListener(this);
zeroButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for 0
zeroButton.setBackground(Color.gray);
jf.add( zeroButton);
equalButton=new JButton("="); //equal button
equalButton.setBounds(460, 430, 80, 80);
equalButton.addActionListener(this);
equalButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for =
equalButton.setBackground(Color.gray);
jf.add(equalButton);
divButton=new JButton("/"); // division buttom
divButton.setBounds(360, 130, 80, 80);
divButton.addActionListener(this);
divButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for /
divButton.setBackground(Color.gray);
jf.add(divButton);
mullButton=new JButton("x"); //multiplication button
mullButton.setBounds(360, 230, 80, 80);
mullButton.addActionListener(this);
mullButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40 for x
mullButton.setBackground(Color.gray);
jf.add(mullButton);
subButton=new JButton("-"); // subtraction button
subButton.setBounds(360, 330, 80, 80);
subButton.addActionListener(this);
subButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40
subButton.setBackground(Color.gray);
jf.add(subButton);
addButton=new JButton("+"); //addition button
addButton.setBounds(360, 430, 80, 80);
addButton.addActionListener(this);
addButton.setFont(new Font("Arial",Font.PLAIN,40)); //Arial 40
addButton.setBackground(Color.gray);
jf.add(addButton);
clearButton=new JButton("CLEAR"); //clear button
clearButton.setBounds(230, 430, 80, 80);
clearButton.addActionListener(this);
clearButton.setFont(new Font("Arial",Font.BOLD,14)); //Arial 14
clearButton.setBackground(Color.gray);
jf.add(clearButton);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //stop running the program when closed the frame
}
public static void main(String[] args) {
new Calculator();
}
}