-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFieldExample2.java
More file actions
59 lines (58 loc) · 1.33 KB
/
Copy pathTextFieldExample2.java
File metadata and controls
59 lines (58 loc) · 1.33 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
import javax.swing.*;
import java.awt.event.*;
public class TextFieldExample2 implements ActionListener {
JTextField tf1,tf2,tf3;
JButton b1,b2;
JLabel l1,l2,l3;
TextFieldExample2(){
JFrame f = new JFrame();
tf1 = new JTextField();
tf1.setBounds(120,50,40,20);
l1 = new JLabel("First Number");
l1.setBounds(30,50,100,20);
tf2 = new JTextField();
tf2.setBounds(140,100,40,20);
l2= new JLabel("Second number");
l2.setBounds(30,100,100,20);
tf3 = new JTextField();
tf3.setBounds(120,150,40,20);
tf3.setEditable(false);
l3 = new JLabel("Result");
l3.setBounds(30,150,80,20);
b1 = new JButton("sum");
b1.setBounds(50,200,80,50);
b2=new JButton("difference");
b2.setBounds(150,200,100,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);
f.add(tf2);
f.add(tf3);
f.add(b1);
f.add(b2);
f.add(l1);
f.add(l2);
f.add(l3);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = tf1.getText();
String s2 = tf2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c =0;
if(e.getSource()==b1) {
c=a+b;
}
else if(e.getSource()==b2) {
c=a-b;
}
String result = String.valueOf(c);
tf3.setText(result);
}
public static void main(String args[]) {
new TextFieldExample2();
}
}