-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathUICALCULATOR.java
117 lines (92 loc) · 2.29 KB
/
UICALCULATOR.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ui.calculator;
/**
*
* @author shubhendu
*/
import java.util.Scanner;
import java.awt.event.*;
import java.awt.*;
public class UICALCULATOR extends Frame implements ActionListener {
TextField tfInput;
Panel panel;
String btnString[] = {"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"C", "0", "=", "/"};
Button btn[] = new Button[16];
int num1 = 0, num2 = 0, result = 0;
char op;
public UICALCULATOR() {
tfInput = new TextField(10);
panel = new Panel();
add(tfInput, "North");
add(panel, "Center");
panel.setLayout(new GridLayout(4,4));
for(int i=0; i < 16; i++) {
btn[i] = new Button(btnString[i]);
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("+")) {
op = '+';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("-")) {
op = '-';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("*")) {
op = '*';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("/")) {
op = '/';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("=")) {
num2 = Integer.parseInt(tfInput.getText());
switch(op) {
case '+' : result = num1 + num2;
break;
case '-' : result = num1 - num2;
break;
case '*' : result = num1 * num2;
break;
case '/' : result = num1 / num2;
break;
}
tfInput.setText(result + "");
result = 0;
}
else if(str.equals("C")) {
tfInput.setText("");
num1 = num2 = result = 0;
}
else {
tfInput.setText(tfInput.getText() + str);
}
}
public static void main(String args[]) {
UICALCULATOR m = new UICALCULATOR ();
m.setTitle("My Calculator");
m.setSize(250,300);
m.setVisible(true);
}
}