-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasiccal.java
More file actions
331 lines (279 loc) · 10.1 KB
/
Basiccal.java
File metadata and controls
331 lines (279 loc) · 10.1 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package GUI;
//Download More code https://github.com/dotLK/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Basiccal {
public static void main(String[] args) {
clabody cal = new clabody();
}
}
class clabody extends JFrame {
double fn , sn , sum ;
String what ;
clabody() {
setVisible(true);
setTitle("My Cal");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
BorderLayout bd = new BorderLayout();
JPanel p1 = new JPanel();
//p1.setBackground(Color.red);
JPanel p2 = new JPanel();
p2.setBackground(Color.GREEN);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
TextField t1 = new TextField(15);
t1.setFont(new Font("", 0, 30));
p1.add(t1);
GridLayout gl = new GridLayout(3, 3);
p2.setLayout(gl);
Button b1 = new Button("1");
// b1.setBackground(Color.red);
p2.add(b1);
b1.setFont(new Font("", 0, 30));
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "1"; // 01231 +
t1.setText(v);
}
});
Button b2 = new Button("2");
//b2.setBackground(Color.BLUE);
p2.add(b2);
b2.setFont(new Font("", 0, 30));
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "2"; // 01231 +
t1.setText(v);
}
});
Button b3 = new Button("3");
// b3.setBackground(Color.GRAY);
p2.add(b3);
b3.setFont(new Font("", 0, 30));
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "3"; // 01231 +
t1.setText(v);
}
});
Button b4 = new Button("4");
// b4.setBackground(Color.ORANGE);
p2.add(b4);
b4.setFont(new Font("", 0, 30));
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "4"; // 01231 +
t1.setText(v);
}
});
Button b5 = new Button("5");
// b5.setBackground(Color.DARK_GRAY);
p2.add(b5);
b5.setFont(new Font("", 0, 30));
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "5"; // 01231 +
t1.setText(v);
}
});
Button b6 = new Button("6");
// b6.setBackground(Color.PINK);
p2.add(b6);
b6.setFont(new Font("", 0, 30));
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "6"; // 01231 +
t1.setText(v);
}
});
Button b7 = new Button("7");
// b7.setBackground(Color.red);
p2.add(b7);
b7.setFont(new Font("", 0, 30));
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "7"; // 01231 +
t1.setText(v);
}
});
Button b8 = new Button("8");
// b8.setBackground(Color.darkGray);
p2.add(b8);
b8.setFont(new Font("", 0, 30));
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "8"; // 01231 +
t1.setText(v);
}
});
Button b9 = new Button("9");
// b9.setBackground(Color.cyan);
p2.add(b9);
b9.setFont(new Font("", 0, 30));
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "9"; // 01231 +
t1.setText(v);
}
});
Button b0 = new Button("0");
p2.add(b0);
b0.setFont(new Font("", 0, 30));
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "0"; // 01231 +
t1.setText(v);
}
});
Button dot = new Button(".");
p2.add(dot);
dot.setFont(new Font("", 0, 30));
dot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText(); //0123
v += "."; // 01231 +
t1.setText(v);
}
});
Button clr = new Button("C");
p2.add(clr);
clr.setFont(new Font("", 0, 30));
clr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t1.setText("");
fn = 0.0;
sn = 0.0;
what= "";
}
});
Button plus = new Button("+");
p2.add(plus);
plus.setFont(new Font("", 0, 30));
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fn = Double.parseDouble(t1.getText()) ;
System.out.print(fn);
t1.setText("");
what = "+";
System.out.print(what);
}
});
Button min = new Button("-");
p2.add(min);
min.setFont(new Font("", 0, 30));
min.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fn = Double.parseDouble(t1.getText()) ;
System.out.print(fn);
t1.setText("");
what = "-";
System.out.print(what);
}
});
Button mul = new Button("*");
p2.add(mul);
mul.setFont(new Font("", 0, 30));
mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fn = Double.parseDouble(t1.getText()) ;
System.out.print(fn);
t1.setText("");
what = "*";
System.out.print(what);
}
});
Button devi = new Button("/");
p2.add(devi);
devi.setFont(new Font("", 0, 30));
devi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fn = Double.parseDouble(t1.getText()) ;
System.out.print(fn);
t1.setText("");
what = "/";
System.out.print(what);
}
});
Button sqr = new Button("√");
p2.add(sqr);
sqr.setFont(new Font("", 0, 30));
sqr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// math.sqtr();
double sq = Double.valueOf(t1.getText());
double ans = Math.sqrt(sq) ;
System.out.println(ans);
t1.setText(String.valueOf(ans));
}
});
Button qe = new Button("=");
p2.add(qe);
qe.setFont(new Font("", 0, 30));
qe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sn = Double.parseDouble(t1.getText()) ;
System.out.print(sn);
sum = cal(fn, sn, what);
System.out.println(" = " +sum);
t1.setText(String.valueOf(sum));
}
});
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("View");
mb.add(m1);
mb.add(m2);
mb.add(m3);
MenuItem m_new = new MenuItem("New File");
MenuItem m_save = new MenuItem("Save");
MenuItem m_Exit = new MenuItem("Exit");
m1.add(m_new);
m1.add(m_save);
m1.add(m_Exit);
m_Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(rootPane, "Hi iam java...!!");
}
});
}
public double cal(double a , double b ,String w){
double tot = 0;
if (w.equals("+")) {
tot = a+ b;
} else if(w.equals("-")) {
tot = a - b;
} else if(w.equals("*")) {
tot = a * b;
} else if(w.equals("/")) {
tot = a / b;
}
return tot ;
}
}