-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator1.java
109 lines (84 loc) · 1.84 KB
/
Calculator1.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator1 implements ActionListener{
JFrame jf;
TextField t1;
TextField t2;
JButton add;
JButton sub;
JButton mul;
JButton div;
JButton clr;
JLabel l;
Calculator1(){
jf= new JFrame("Calculator");
jf.setSize(800,800);
jf.setLayout(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l=new JLabel();
l.setBounds(100,400,100,100);
t1=new TextField();
t1.setBounds(100,100,350,50);
t2=new TextField();
t2.setBounds(100,200,350,50);
add=new JButton("+");
add.setBounds(100,300,80,80);
add.addActionListener(this);
sub=new JButton("-");
sub.setBounds(180,300,80,80);
sub.addActionListener(this);
mul=new JButton("*");
mul.setBounds(260,300,80,80);
mul.addActionListener(this);
div=new JButton("/");
div.setBounds(340,300,80,80);
div.addActionListener(this);
clr=new JButton("clear");
clr.setBounds(420,300,80,80);
clr.addActionListener(this);
jf.add(t1);
jf.add(t2);
jf.add(add);
jf.add(sub);
jf.add(mul);
jf.add(div);
jf.add(clr);
jf.add(l);
jf.setVisible(true);
}
public static void main(String args[]){
Calculator1 calculator = new Calculator1();
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == add){
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int r=a+b;
l.setText("Result : "+r+" ");
}
else if(e.getSource() == sub){
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int r=a-b;
l.setText("Result : "+r+" ");
}
else if(e.getSource() == mul){
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int r=a*b;
l.setText("Result : "+r+" ");
}
else if(e.getSource() == div){
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int r=a/b;
l.setText("Result : "+r+" ");
}
else if(e.getSource() == clr){
t1.setText("");
t2.setText("");
l.setText("");
}
}
}