-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaCalculator.java
199 lines (195 loc) · 7.52 KB
/
JavaCalculator.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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JavaCalculator {
private double total1 = 0.0;
private double total2 = 0.0;
private char operator;
private JPanel javaCalculator;
private JTextField textField1;
private JButton btnMultiply;
private JButton btnOne;
private JButton btnTwo;
private JButton btnFour;
private JButton btnSeven;
private JButton btnZero;
private JButton btnMinus;
private JButton btnFive;
private JButton btnEight;
private JButton btnPoint;
private JButton btnPlus;
private JButton btnDivide;
private JButton btnEquals;
private JButton btnThree;
private JButton btnSix;
private JButton btnNine;
private JButton btnClear;
private void getOperator(String btnText){
operator = btnText.charAt(0);
total1 = total1 + Double.parseDouble(textField1.getText());
textField1.setText("");
}
public JavaCalculator() {
btnOne.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnOneText = textField1.getText() + btnOne.getText();
textField1.setText(btnOneText);
}
});
btnTwo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnTwoText = textField1.getText() + btnTwo.getText();
textField1.setText(btnTwoText);
}
});
btnThree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnThreeText = textField1.getText() + btnThree.getText();
textField1.setText(btnThreeText);
}
});
btnFour.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnFourText = textField1.getText() + btnFour.getText();
textField1.setText(btnFourText);
}
});
btnFive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnFiveText = textField1.getText() + btnFive.getText();
textField1.setText(btnFiveText);
}
});
btnSix.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnSixText = textField1.getText() + btnSix.getText();
textField1.setText(btnSixText);
}
});
btnSeven.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnSevenText = textField1.getText() + btnSeven.getText();
textField1.setText(btnSevenText);
}
});
btnEight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnEightText = textField1.getText() + btnEight.getText();
textField1.setText(btnEightText);
}
});
btnNine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnNineText = textField1.getText() + btnNine.getText();
textField1.setText(btnNineText);
}
});
btnZero.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String btnZeroText = textField1.getText() + btnZero.getText();
textField1.setText(btnZeroText);
}
});
// btnPlus.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// String btnOneText = textField1.getText() + btnPlus.getText();
// textField1.setText(btnOneText);
// }
// });
btnPoint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textField1.getText().equals("")){
textField1.setText("0.");
}
else if (textField1.getText().contains(".")){
btnPoint.setEnabled(false);
}
else{
String btnPointText = textField1.getText() + btnPoint.getText();
textField1.setText(btnPointText);
}
btnPoint.setEnabled(true);
}
});
btnMinus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = btnMinus.getText();
getOperator(buttonText);
}
});
btnDivide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = btnDivide.getText();
getOperator(buttonText);
}
});
btnMultiply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = btnMultiply.getText();
getOperator(buttonText);
}
});
btnPlus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = btnPlus.getText();
getOperator(buttonText);
}
});
btnEquals.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (operator){
case '+':
total2 = total1 + Double.parseDouble(textField1.getText());
break;
case '-':
total2 = total1 - Double.parseDouble(textField1.getText());
break;
case '*':
total2 = total1 * Double.parseDouble(textField1.getText());
break;
case '/':
total2 = total1 / Double.parseDouble(textField1.getText());
break;
default:
textField1.setText("Invalid Operator");
}
textField1.setText(Double.toString(total2));
total1 = 0;
}
});
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
total2 = 0;
textField1.setText("");
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("JavaCalculator");
frame.setContentPane(new JavaCalculator().javaCalculator);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void createUIComponents() {
// TODO: place custom component creation code here
}
}