-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwingTest.java
78 lines (56 loc) · 1.46 KB
/
SwingTest.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
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
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class SwingTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
create();
}
});
}
private static void create() {
JFrame frame = new JFrame();
// Add the JPanel to this frame
frame.setContentPane(new Application());
frame.setSize(250, 150);
frame.setLocationRelativeTo(null);
// Exit application if the frame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// And off we go!
frame.setVisible(true);
}
}
class Application extends JPanel {
private JTextField input;
private JLabel output;
private JButton button;
Application() {
super();
initialize();
}
private void initialize() {
input = new JTextField(20);
add(input);
output = new JLabel("Enter an Integer");
add(output);
button = new JButton("Calculate Square");
button.addActionListener(new ButtonActionListener());
add(button);
}
class ButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
long n = Long.parseLong(input.getText());
output.setText("Square: " + (n * n));
} catch (NumberFormatException ex) {
output.setText("Invalid integer specified");
}
}
}
}