-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyFrame.java
51 lines (43 loc) · 1.23 KB
/
MyFrame.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
//Java swing example that performs sqare of the input number
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyFrame extends JFrame implements ActionListener {
JLabel l, l1;
JTextField num, sqrt;
JButton ok, clear;
public MyFrame() {
l = new JLabel("Number: ");
l1 = new JLabel("Square: ");
num = new JTextField(20);
sqrt = new JTextField(20);
ok = new JButton("OK");
clear = new JButton("Refresh");
setLayout(new FlowLayout());
add(l);
add(num);
add(l1);
add(sqrt);
ok.addActionListener(this);
clear.addActionListener(this);
add(ok);
add(clear);
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
String n = num.getText();
int ni = Integer.parseInt(n);
int sq = ni * ni;
sqrt.setText("" + sq);
} else if (e.getSource() == clear) {
num.setText("");
sqrt.setText("");
}
}
public static void main(String[] args) {
new MyFrame();
}
}