-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
182 lines (153 loc) · 6.02 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator {
private JFrame frame;
private JTextField textField;
private double first, second, result;
private String operation;
private static final String CALCULATOR_VERSION = "1.0";
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
new Calculator().initialize();
} catch (Exception e) {
e.printStackTrace();
}
});
}
public Calculator() {
frame = new JFrame("Calculator v" + CALCULATOR_VERSION);
frame.setSize(376, 576);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().setLayout(null);
}
public void initialize() {
textField = createTextField();
frame.getContentPane().add(textField);
createRadioButtons();
createButtons();
frame.setVisible(true);
}
private JTextField createTextField() {
JTextField tf = new JTextField();
tf.setHorizontalAlignment(SwingConstants.RIGHT);
tf.setFont(new Font("Tahoma", Font.PLAIN, 32));
tf.setBounds(12, 13, 348, 82);
tf.setColumns(10);
return tf;
}
private void createRadioButtons() {
ButtonGroup btg = new ButtonGroup();
JRadioButton onButton = new JRadioButton("On");
onButton.setFont(new Font("Tahoma", Font.PLAIN, 17));
onButton.setBounds(12, 108, 74, 32);
onButton.setSelected(true);
JRadioButton offButton = new JRadioButton("Off");
offButton.setFont(new Font("Tahoma", Font.PLAIN, 17));
offButton.setBounds(12, 145, 74, 32);
btg.add(onButton);
btg.add(offButton);
frame.getContentPane().add(onButton);
frame.getContentPane().add(offButton);
onButton.addActionListener(e -> setComponentsEnabled(true));
offButton.addActionListener(e -> setComponentsEnabled(false));
}
private JRadioButton createRadioButton(String text, int x, int y, int width, int height, boolean selected) {
JRadioButton radioButton = new JRadioButton(text);
radioButton.setFont(new Font("Tahoma", Font.PLAIN, 17));
radioButton.setBounds(x, y, width, height);
radioButton.setSelected(selected);
frame.getContentPane().add(radioButton);
return radioButton;
}
private void setCalculatorState(boolean state) {
frame.setEnabled(state);
textField.setEnabled(state);
Component[] components = frame.getContentPane().getComponents();
for (Component component : components) {
if (component instanceof JButton) {
component.setEnabled(state);
}
}
}
private void createButtons() {
createButton("<-", 94, 108, 72, 69, this::backspace);
createButton("C", 178, 108, 85, 69, () -> textField.setText(null));
createOperationButton("+", 275, 108, 85, 69, "+");
for (int i = 1; i <= 9; i++) {
createDigitButton(String.valueOf(i), (i - 1) % 3 * 97, 190 + (i - 1) / 3 * 88, 85, 75);
}
createOperationButton("-", 297, 190, 63, 75, "-");
createDigitButton("0", 12, 454, 56, 75);
createOperationButton("*", 297, 278, 63, 75, "x");
createOperationButton("/", 297, 366, 63, 75, "/");
createButton("%", 149, 454, 74, 75, () -> performOperation("%"));
createButton(".", 81, 454, 56, 75, () -> textField.setText(textField.getText() + "."));
JButton equalsButton = createButton("=", 236, 454, 124, 75, this::calculateResult);
equalsButton.setFont(new Font("Tahoma", Font.BOLD, 28));
}
private void setComponentsEnabled(boolean state) {
Component[] components = frame.getContentPane().getComponents();
for (Component component : components) {
if (component instanceof JButton || component instanceof JTextField) {
component.setEnabled(state);
}
}
}
private void createOperationButton(String label, int x, int y, int width, int height, String op) {
createButton(label, x, y, width, height, () -> performOperation(op));
}
private void createDigitButton(String label, int x, int y, int width, int height) {
createButton(label, x, y, width, height, () -> textField.setText(textField.getText() + label));
}
private JButton createButton(String label, int x, int y, int width, int height, Runnable action) {
JButton button = new JButton(label);
button.addActionListener(e -> action.run());
button.setFont(new Font("Tahoma", Font.BOLD, 22));
button.setBounds(x, y, width, height);
frame.getContentPane().add(button);
return button;
}
private void backspace() {
String text = textField.getText();
if (text.length() > 0) {
textField.setText(text.substring(0, text.length() - 1));
}
}
private void performOperation(String op) {
try {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = op;
} catch (Exception e) {
e.printStackTrace();
}
}
private void calculateResult() {
try {
second = Double.parseDouble(textField.getText());
switch (operation) {
case "+":
result = first + second;
break;
case "-":
result = first - second;
break;
case "x":
result = first * second;
break;
case "/":
result = first / second;
break;
case "%":
result = first % second;
break;
}
textField.setText(String.format("%.2f", result));
} catch (Exception e) {
e.printStackTrace();
}
}
}