-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.java
More file actions
83 lines (63 loc) · 1.98 KB
/
Math.java
File metadata and controls
83 lines (63 loc) · 1.98 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MathOperation implements ActionListener {
JLabel j1, j2, j3;
JTextField jtf1, jtf2, jtf3;
JButton jadd, jsub;
MathOperation() {
JFrame jf = new JFrame("Math Operation");
jf.setSize(400, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(null);
j1 = new JLabel("First Number:");
j2 = new JLabel("Second Number:");
j3 = new JLabel("Result:");
j1.setBounds(30, 30, 100, 25);
j2.setBounds(30, 70, 100, 25);
j3.setBounds(30, 110, 100, 25);
jtf1 = new JTextField();
jtf2 = new JTextField();
jtf3 = new JTextField();
jtf3.setEditable(false);
jtf1.setBounds(140, 30, 150, 25);
jtf2.setBounds(140, 70, 150, 25);
jtf3.setBounds(140, 110, 150, 25);
jadd = new JButton("ADD");
jsub = new JButton("SUB");
jadd.setBounds(80, 160, 80, 30);
jsub.setBounds(180, 160, 80, 30);
jadd.addActionListener(this);
jsub.addActionListener(this);
jf.add(j1);
jf.add(j2);
jf.add(j3);
jf.add(jtf1);
jf.add(jtf2);
jf.add(jtf3);
jf.add(jadd);
jf.add(jsub);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
try {
int x = Integer.parseInt(jtf1.getText());
int y = Integer.parseInt(jtf2.getText());
int result;
if (ae.getSource() == jadd) {
result = x + y;
} else {
result = x - y;
}
jtf3.setText(String.valueOf(result));
} catch (NumberFormatException e) {
jtf3.setText("Invalid Input");
}
}
}
public class Math {
public static void main(String[] args) {
new MathOperation();
}
}