-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUICalculator.java
162 lines (153 loc) · 4.91 KB
/
GUICalculator.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
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Calc extends JFrame implements ActionListener {
JFrame f;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, bc, be;
JTextField t;
double a, b, r;
int op ;
Calc() {
f = new JFrame("Calculator");
f.setLayout(null);
f.setVisible(true);
f.setBounds(50, 50, 400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t = new JTextField();
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
be = new JButton("=");
bc = new JButton("clr");
badd = new JButton("+");
bsub = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
b1.setBounds(60, 60, 50, 50);
b2.setBounds(120, 60, 50, 50);
b3.setBounds(180, 60, 50, 50);
b4.setBounds(240, 60, 50, 50);
b5.setBounds(60, 120, 50, 50);
b6.setBounds(120, 120, 50, 50);
b7.setBounds(180, 120, 50, 50);
b8.setBounds(240, 120, 50, 50);
b9.setBounds(60, 180, 50, 50);
b0.setBounds(120, 180, 50, 50);
be.setBounds(180, 180, 50, 50);
bc.setBounds(240, 180, 50, 50);
badd.setBounds(60, 240, 50, 50);
bsub.setBounds(120, 240, 50, 50);
bmul.setBounds(180, 240, 50, 50);
bdiv.setBounds(240, 240, 50, 50);
t.setBounds(60, 20, 230, 30);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
be.addActionListener(this);
bc.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
f.add(t);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b0);
f.add(be);
f.add(bc);
f.add(badd);
f.add(bsub);
f.add(bmul);
f.add(bdiv);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
t.setText(t.getText().concat("1"));
} else if (e.getSource() == b2) {
t.setText(t.getText().concat("2"));
} else if (e.getSource() == b3) {
t.setText(t.getText().concat("3"));
} else if (e.getSource() == b4) {
t.setText(t.getText().concat("4"));
} else if (e.getSource() == b5) {
t.setText(t.getText().concat("5"));
} else if (e.getSource() == b6) {
t.setText(t.getText().concat("6"));
} else if (e.getSource() == b7) {
t.setText(t.getText().concat("7"));
} else if (e.getSource() == b8) {
t.setText(t.getText().concat("8"));
} else if (e.getSource() == b9) {
t.setText(t.getText().concat("9"));
} else if (e.getSource() == b0) {
t.setText(t.getText().concat("0"));
} else if (e.getSource() == badd) {
a = Double.parseDouble(t.getText());
op = 1;
t.setText(null);
} else if (e.getSource() == bsub) {
a = Double.parseDouble(t.getText());
op = 2;
t.setText(null);
} else if (e.getSource() == bmul) {
a = Double.parseDouble(t.getText());
op = 3;
t.setText(null);
} else if (e.getSource() == bdiv) {
a = Double.parseDouble(t.getText());
op = 4;
t.setText(null);
} else if (e.getSource() == be) {
b = Double.parseDouble(t.getText());
switch (op) {
case 1:
r = a + b;
break;
case 2:
r = a - b;
break;
case 3:
r = a * b;
break;
case 4:
if (b != 0) {
r = a / b;
} else {
t.setText("Error");
return;
}
break;
}
t.setText(""+r);
} else if (e.getSource() == bc) {
t.setText(null);
}
}
}
class Calculator {
public static void main(String[] args){
Calc c = new Calc();
}
}