forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeSquare.java
More file actions
43 lines (39 loc) · 1.06 KB
/
ComputeSquare.java
File metadata and controls
43 lines (39 loc) · 1.06 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
package chapter2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class ComputeSquare extends JFrame
{
JTextField number;
JTextField square;
JButton submit;
JPanel panel;
public ComputeSquare()
{
number = new JTextField(10);
square = new JTextField(10);
submit = new JButton("Submit");
submit.addActionListener( x -> {
try {
int num = Integer.parseInt(number.getText());
square.setText(Integer.toString(num*num));
}
catch (NumberFormatException e) {
System.out.println(number.getText()
+ " is not a number");
}
});
panel = new JPanel();
setSize(200,130);
panel.add(number);
panel.add(square);
panel.add(submit);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new ComputeSquare();
}
}