-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
182 lines (165 loc) · 5.34 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 javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Calculator implements ActionListener {
private int state, i, att[][];
private boolean input;
private double result, num_front, num_back;
private JFrame frame;
private JButton[] num = new JButton[10];
private JTextField display = new JTextField("0");
private ArrayList<JComponent> GUI_Component;
String[] symbol = {"+", "-", "*", "/", "=", ".", "C"};
public Calculator() {
init();
run();
}
private void init() {
state = 0;
input = false;
result = num_front = num_back = 0;
int fill[] = {
GridBagConstraints.BOTH,
GridBagConstraints.VERTICAL,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.NONE
};
int anchor[] = {
GridBagConstraints.CENTER,
GridBagConstraints.EAST,
GridBagConstraints.SOUTHEAST,
GridBagConstraints.SOUTH,
GridBagConstraints.SOUTHWEST,
GridBagConstraints.WEST,
GridBagConstraints.NORTHWEST,
GridBagConstraints.NORTH,
GridBagConstraints.NORTHEAST
};
int a[][] = {
{0, 0, 3, 1, 1, 1, fill[0], anchor[2]},
{0, 4, 1, 1, 1, 1, fill[0], anchor[0]},
{0, 3, 1, 1, 1, 1, fill[0], anchor[0]},
{1, 3, 1, 1, 1, 1, fill[0], anchor[0]},
{2, 3, 1, 1, 1, 1, fill[0], anchor[0]},
{0, 2, 1, 1, 1, 1, fill[0], anchor[0]},
{1, 2, 1, 1, 1, 1, fill[0], anchor[0]},
{2, 2, 1, 1, 1, 1, fill[0], anchor[0]},
{0, 1, 1, 1, 1, 1, fill[0], anchor[0]},
{1, 1, 1, 1, 1, 1, fill[0], anchor[0]},
{2, 1, 1, 1, 1, 1, fill[0], anchor[0]},
{3, 1, 1, 1, 1, 1, fill[0], anchor[0]},
{3, 2, 1, 1, 1, 1, fill[0], anchor[0]},
{3, 3, 1, 1, 0, 0, fill[0], anchor[0]},
{3, 4, 1, 1, 0, 0, fill[0], anchor[0]},
{2, 4, 1, 1, 0, 0, fill[0], anchor[0]},
{1, 4, 1, 1, 0, 0, fill[0], anchor[0]},
{3, 0, 1, 1, 0, 0, fill[0], anchor[0]}
};
att = a;
GUI_Component = new ArrayList<JComponent>(17);
}
public void addComponent(int i) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = att[i][0];
c.gridy = att[i][1];
c.gridwidth = att[i][2];
c.gridheight = att[i][3];
c.weightx = att[i][4];
c.weighty = att[i][5];
c.fill = att[i][6];
c.anchor = att[i][7];
frame.add(GUI_Component.get(i), c);
}
public void run() {
frame = new JFrame("Calcultator");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
display.setEditable(false);
GUI_Component.add(display);
for (i = 0; i < 10; i++) {
num[i] = new JButton("" + i);
num[i].addActionListener(this);
GUI_Component.add(num[i]);
}
for (String sym: symbol) {
JButton sym_button = new JButton("" + sym);
sym_button.addActionListener(this);
GUI_Component.add(sym_button);
}
for (i = 0; i < GUI_Component.size(); i++) {
addComponent(i);
}
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("+")) {
if (this.input) {
op();
}
this.state = 1;
} else if (s.equals("-")) {
if (this.input) {
op();
}
this.state = 2;
} else if (s.equals("*")) {
if (this.input) {
op();
}
this.state = 3;
} else if (s.equals("/")) {
if (this.input) {
op();
}
this.state = 4;
} else if (s.equals("=")) {
if (this.input) {
op();
}
this.state = 0;
} else if (s.equals("C")) {
this.num_back = 0;
this.display.setText("" + 0);
this.input = true;
} else {
if (!input) {
if (s.equals(".")) {
display.setText("0.");
} else {
display.setText("" + s);
}
input = true;
} else {
display.setText(display.getText() + s);
}
}
}
private void op() {
this.num_back= Double.parseDouble(this.display.getText());
switch (this.state) {
case 0:
this.num_front = this.num_back;
break;
case 1:
this.num_front += this.num_back;
break;
case 2:
this.num_front -= this.num_back;
break;
case 3:
this.num_front *= this.num_back;
break;
case 4:
this.num_front /= this.num_back;
break;
}
this.display.setText("" + this.num_front);
this.input = false;
}
public static void main(String[] args) {
new Calculator();
}
}