-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorLib.java
294 lines (238 loc) · 8.84 KB
/
CalculatorLib.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package org.com.Calc;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
public class CalculatorLib implements ActionListener{
JFrame frame=new JFrame();//used to build component like label,button,etc to crate GUI
JPanel panel=new JPanel();// it w ill provide the screen for user input
JTextArea textarea=new JTextArea(2,10);//use to provide column n row in text area 2=row and 10=column(10digit)
JButton Button1=new JButton("1");
JButton Button2=new JButton("2");
JButton Button3=new JButton("3");
JButton Button4=new JButton("4");
JButton Button5=new JButton("5");
JButton Button6=new JButton("6");
JButton Button7=new JButton("7");
JButton Button8=new JButton("8");
JButton Button9=new JButton("9");
JButton Button0=new JButton("0");
JButton Buttonadd=new JButton("+");
JButton Buttonsub=new JButton("-");
JButton Buttonmul=new JButton("*");
JButton Buttondiv=new JButton("/");
JButton Buttonclear=new JButton("c");
JButton Buttondot=new JButton(".");
JButton Buttonequal=new JButton("=");
private static int number1;
private static int number11;
private static int number2;
private static String action;
private static float result;
public CalculatorLib() {
frame.setSize(350,450); //to set the frame size width and height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//to show the close option
frame.setVisible(true);//to show the frame
frame.setTitle("Calculator");
// frame.setResizable(false);//frame size is frized
frame.add(panel);//here panel is added to the frame white screen
panel.setBackground(Color.gray);//here panel's color is set as white
Border border=BorderFactory.createLineBorder(Color.RED,4);
panel.setBorder(border);
panel.add(textarea);
textarea.setBackground(Color.WHITE);
Border tborder=BorderFactory.createLineBorder(Color.BLACK,3);
textarea.setBorder(tborder);
Font font=new Font("arial",Font.BOLD,33);
textarea.setFont(font);
textarea.setForeground(Color.BLACK);
textarea.setPreferredSize(new Dimension(2,10));
textarea.setLineWrap(true);
Button1.setPreferredSize(new Dimension(100,40));
panel.add(Button1);
Button1.setEnabled(true);
Button1.setBackground(Color.WHITE);
// Button1.setBorder(BorderFactory.creatLineBorder(Color.BLACK,3));
Button2.setPreferredSize(new Dimension(100,40));
panel.add(Button2);
Button2.setEnabled(true);
Button2.setBackground(Color.RED);
Button3.setPreferredSize(new Dimension(100,40));
panel.add(Button3);
Button3.setEnabled(true);
Button3.setBackground(Color.RED);
Button4.setPreferredSize(new Dimension(100,40));
panel.add(Button4);
Button4.setEnabled(true);
Button4.setBackground(Color.RED);
Button5.setPreferredSize(new Dimension(100,40));
panel.add(Button5);
Button5.setEnabled(true);
Button5.setBackground(Color.RED);
Button6.setPreferredSize(new Dimension(100,40));
panel.add(Button6);
Button6.setEnabled(true);
Button6.setBackground(Color.RED);
Button7.setPreferredSize(new Dimension(100,40));
panel.add(Button7);
Button7.setEnabled(true);
Button7.setBackground(Color.RED);
Button8.setPreferredSize(new Dimension(100,40));
panel.add(Button8);
Button8.setEnabled(true);
Button8.setBackground(Color.RED);
Button9.setPreferredSize(new Dimension(100,40));
panel.add(Button9);
Button9.setEnabled(true);
Button9.setBackground(Color.RED);
Button0.setPreferredSize(new Dimension(100,40));
panel.add(Button0);
Button0.setEnabled(true);
Button0.setBackground(Color.RED);
Buttonadd.setPreferredSize(new Dimension(100,40));
panel.add(Buttonadd);
Buttonadd.setEnabled(true);
Buttonadd.setBackground(Color.RED);
Buttonsub.setPreferredSize(new Dimension(100,40));
panel.add(Buttonsub);
Buttonsub.setEnabled(true);
Buttonsub.setBackground(Color.RED);
Buttonmul.setPreferredSize(new Dimension(100,40));
panel.add(Buttonmul);
Buttonmul.setEnabled(true);
Buttonmul.setBackground(Color.RED);
Buttondiv.setPreferredSize(new Dimension(100,40));
panel.add(Buttondiv);
Buttondiv.setEnabled(true);
Buttondiv.setBackground(Color.RED);
Buttondot.setPreferredSize(new Dimension(100,40));
panel.add(Buttondot);
Buttondot.setEnabled(true);
Buttondot.setBackground(Color.RED);
Buttonequal.setPreferredSize(new Dimension(100,40));
panel.add(Buttonequal);
Buttonequal.setEnabled(true);
Buttonequal.setBackground(Color.RED);
Buttonclear.setPreferredSize(new Dimension(100,40));
panel.add(Buttonclear);
Buttonclear.setEnabled(true);
Buttonclear.setBackground(Color.RED);
Button1.addActionListener((ActionListener) this);
Button2.addActionListener((ActionListener) this);
Button3.addActionListener((ActionListener) this);
Button4.addActionListener((ActionListener) this);
Button5.addActionListener((ActionListener) this);
Button6.addActionListener((ActionListener) this);
Button7.addActionListener((ActionListener) this);
Button8.addActionListener((ActionListener) this);
Button9.addActionListener((ActionListener) this);
Button0.addActionListener((ActionListener) this);
Buttonadd.addActionListener((ActionListener) this);
Buttonsub.addActionListener((ActionListener) this);
Buttonmul.addActionListener((ActionListener) this);
Buttondiv.addActionListener((ActionListener) this);
Buttondot.addActionListener((ActionListener) this);
Buttonequal.addActionListener((ActionListener) this);
Buttonclear.addActionListener((ActionListener) this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==Buttonclear) {
number1=0;
number2=0;
textarea.setText("");
}
if(source==Button1) {
String s=textarea.getText()+Button1.getText();
textarea.setText(s);
}
if(source==Button2) {
String s=textarea.getText()+Button2.getText();
textarea.setText(s);
}
if(source==Button3) {
String s=textarea.getText()+Button3.getText();
textarea.setText(s);
}
if(source==Button4) {
String s=textarea.getText()+Button4.getText();
textarea.setText(s);
}
if(source==Button5) {
String s=textarea.getText()+Button5.getText();
textarea.setText(s);
}
if(source==Button6) {
String s=textarea.getText()+Button6.getText();
textarea.setText(s);
}
if(source==Button7) {
String s=textarea.getText()+Button7.getText();
textarea.setText(s);
}
if(source==Button8) {
String s=textarea.getText()+Button8.getText();
textarea.setText(s);
}
if(source==Button9) {
String s=textarea.getText()+Button9.getText();
textarea.setText(s);
}
if(source==Button0) {
String s=textarea.getText()+Button0.getText();
textarea.setText(s);
}
if(source==Buttondot) {
String s=textarea.getText()+Buttondot.getText();
textarea.setText(s);
}
if(source==Buttonadd) {
action=Buttonadd.getText();
number1=Integer.valueOf(textarea.getText());
textarea.setText("");
}
if(source==Buttondiv) {
action=Buttondiv.getText();
number1=Integer.valueOf(textarea.getText());
textarea.setText("");
}
if (source==Buttonmul) {
action=Buttonmul.getText();
number1=Integer.valueOf(textarea.getText());
textarea.setText("");
}
if (source==Buttonsub) {
action=Buttonsub.getText();
number1=Integer.valueOf(textarea.getText());
textarea.setText("");
}
if(source==Buttonequal) {
number2=Integer.valueOf(textarea.getText());
if(action=="+") {
result=(number1+number2);
number11=(int)result;
textarea.setText(number1+action+number2+""+"="+result);
}
else if(action=="-") {
result=number1-number2;
textarea.setText(number1+action+number2+""+"="+result);
}
else if(action=="*") {
result=number1*number2;
textarea.setText(number1+action+number2+""+"="+result);
}
else if(action=="/") {
result=number1/number2;
textarea.setText(number1+action+number2+""+"="+result);
}
}
}
}