forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame_java.java
More file actions
47 lines (44 loc) · 982 Bytes
/
Copy pathFrame_java.java
File metadata and controls
47 lines (44 loc) · 982 Bytes
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyFrame extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JTextField tf1,tf2,tf3;
JButton b1,b2;
MyFrame()
{
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(new GridLayout(4,2));
l1=new JLabel("No.1");
l2=new JLabel("No.2");
l3=new JLabel("Result");
tf1=new JTextField();
tf2=new JTextField();
tf3=new JTextField();
b1=new JButton("Sum");
b2=new JButton("Avg");
b1.addActionListener(this);
b2.addActionListener(this);
add(l1);add(tf1);
add(l2);add(tf2);
add(l3);add(tf3);
add(b1);add(b2);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
new MyFrame();
}
public void actionPerformed(ActionEvent ae)
{
int x=Integer.parseInt(tf1.getText());
int y=Integer.parseInt(tf2.getText());
JButton b=(JButton)ae.getSource();
if(b==b1)
tf3.setText(x+y+"");
else
tf3.setText((x+y)/2.0f+"");
}
}