Java Reference
In-Depth Information
Display 17.4
The Normal Way to Dei ne a
JFrame
1
import
javax.swing.JFrame;
2
import
javax.swing.JButton;
3
public class
FirstWindow
extends
JFrame
4 {
5
public static final int
WIDTH = 300;
6
public static final int
HEIGHT = 200;
7
public
FirstWindow()
8 {
9
super
();
10
setSize(WIDTH, HEIGHT);
11
setTitle("First Window Class");
12
setDefaultCloseOperation(
13
JFrame.DO_NOTHING_ON_CLOSE);
14
JButton endButton =
new
JButton("Click to end program.");
15
endButton.addActionListener(
new
EndingListener());
16
add(endButton);
17 }
18 }
The class
EndingListener
is defined in Display 17.2 .
This is the file
FirstWindow.java
.
This is the file
DemoWindow.java
.
1
public class
DemoWindow
2 {
3
public static void
main(String[] args)
4 {
5 FirstWindow w =
new
FirstWindow();
6 w.setVisible(
true
);
7 }
8 }
R
ESULTING
GUI