-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
71 lines (66 loc) · 1.64 KB
/
Calculator.java
File metadata and controls
71 lines (66 loc) · 1.64 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
package UNIT_04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class Calculator extends JFrame implements ActionListener{
JTextField tf1;
JTextField tf2;
JTextField tf3;
Calculator()
{
Scanner sc=new Scanner(System.in);
JLabel l1=new JLabel("Number 1:");
JLabel l2=new JLabel("Number 2:");
JLabel l3=new JLabel("Output:");
JButton b1=new JButton("Sum");
JButton b2=new JButton("Subtract");
JButton b3=new JButton("Mul");
JButton b4=new JButton("Div");
tf1 =new JTextField(10);
tf2 =new JTextField(10);
tf3 =new JTextField(10);
setLayout(new FlowLayout());
add(l1);
add(tf1);
add(l2);
add(tf2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
add(tf3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
int res=0;
if(s.equals("Sum"))
{
res=Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText());
}
if(s.equals("Subtract"))
{
res=Integer.parseInt(tf1.getText())-Integer.parseInt(tf2.getText());
}
if(s.equals("Mul"))
{
res=Integer.parseInt(tf1.getText())*Integer.parseInt(tf2.getText());
}
if(s.equals("Div"))
{
res=Integer.parseInt(tf1.getText())/Integer.parseInt(tf2.getText());
}
tf3.setText(Integer.toString(res));
}
public static void main(String[] args)
{
Calculator d=new Calculator();
d.setSize(400,400);
d.setVisible(true);
}
}