-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCalculator.java
101 lines (78 loc) · 2.67 KB
/
SimpleCalculator.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
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame{
JLabel l1,l2;
JTextField t1,t2;
JButton b1,b2,b3,b4;
Calculator(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
l1 = new JLabel("Simple Calculater");
l1.setFont(new Font("Time New Roman", Font.BOLD, 30));
l1.setForeground(Color.BLUE);
l1.setBounds(60,10,500,30);
add(l1);
t1 = new JTextField(60);
t2 = new JTextField(60);
b1 = new JButton("Add");
b2 = new JButton("Sub");
b3 = new JButton("Mul");
b4 = new JButton("Div");
t1.setBounds(100,60,120,30);
// l2.setFont(new Font("Time New Roman", Font.ITALIC, 20));
t2.setBounds(100,100,120,30);
b1.setBounds(100,140,60,30);
b2.setBounds(160,140,60,30);
b3.setBounds(100,180,60,30);
b4.setBounds(160,180,60,30);
l2 = new JLabel("Result : -");
l2.setBounds(250,80,500,30);
l2.setFont(new Font("Time New Roman", Font.ITALIC, 20));
add(l2);
add(b1);
add(b2);
add(b3);
add(b4);
add(t1);
add(t2);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int no1 = Integer.parseInt(t1.getText());
int no2 = Integer.parseInt(t2.getText());
l2.setText("Result : " + (no1+no2));
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int no1 = Integer.parseInt(t1.getText());
int no2 = Integer.parseInt(t2.getText());
l2.setText("Result : " + (no1-no2));
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int no1 = Integer.parseInt(t1.getText());
int no2 = Integer.parseInt(t2.getText());
l2.setText("Result : " + (no1*no2));
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int no1 = Integer.parseInt(t1.getText());
int no2 = Integer.parseInt(t2.getText());
l2.setText("Result : " + (no1/no2));
}
});
}
}
class SimpleCalculator {
public static void main(String[] args) {
Calculater c = new Calculater();
c.setBounds(400,200,600,300);
c.setVisible(true);
}
}