-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUICalculator.java
More file actions
100 lines (81 loc) · 2.2 KB
/
GUICalculator.java
File metadata and controls
100 lines (81 loc) · 2.2 KB
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
import java.awt.*;
import java.awt.event.*;
class GUICalculator extends Frame implements ActionListener {
TextField text;
Panel panel;
String button[] = { "7", "8", "9", "+", "4", "5", "6", "—", "1", "2", "3", "*", "AC", "0", "/", "=" };
Button btn[] = new Button[16];
int A = 0, B = 0, output = 0;
char opt;
public GUICalculator() {
Font f = new Font("Helvetica", Font.PLAIN, 20);
text = new TextField(10);
text.setFont(f);
panel = new Panel();
add(text, "North");
add(panel, "Center");
panel.setLayout(new GridLayout(4, 4));
for (int i = 0; i < 16; i++) {
btn[i] = new Button(button[i]);
btn[i].setFont(f);
btn[i].addActionListener(this);
panel.add(btn[i]);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals("+")) {
opt = '+';
A = Integer.parseInt(text.getText());
text.setText("");
} else if (str.equals("—")) {
opt = '—';
A = Integer.parseInt(text.getText());
text.setText("");
} else if (str.equals("*")) {
opt = '*';
A = Integer.parseInt(text.getText());
text.setText("");
} else if (str.equals("/")) {
opt = '/';
A = Integer.parseInt(text.getText());
text.setText("");
} else if (str.equals("=")) {
B = Integer.parseInt(text.getText());
switch (opt) {
case '+':
output = A + B;
break;
case '—':
output = A - B;
break;
case '*':
output = A * B;
break;
case '/':
output = A / B;
break;
}
text.setText(output + "");
output = 0;
} else if (str.equals("AC")) {
text.setText("");
A = B = output = 0;
} else {
text.setText(text.getText() + str);
}
}
public static void main(String args[]) {
GUICalculator m = new GUICalculator();
m.setTitle("GUI — Calculator");
m.setSize(300, 350);
m.setBackground(Color.PINK);
m.setForeground(Color.BLACK);
m.setVisible(true);
}
}