Java Reference
In-Depth Information
Display 17.2
A First Swing Demonstration Program
(part 1 of 2)
1
import
javax.swing.JFrame;
This program is not typical of the
style we will use in Swing
programs.
2
import
javax.swing.JButton;
3
public class
FirstSwingDemo
4{
5
public static final int
WIDTH = 300;
6
public static final int
HEIGHT = 200;
7
public static void
main(String[] args)
8
{
9
JFrame firstWindow =
new
JFrame();
10
firstWindow.setSize(WIDTH, HEIGHT);
11
firstWindow.setDefaultCloseOperation(
12
JFrame.DO_NOTHING_ON_CLOSE);
13
JButton endButton =
new
JButton("Click to end program.");
14
EndingListener buttonEar =
new
EndingListener();
15
endButton.addActionListener(buttonEar);
16
firstWindow.add(endButton);
17
firstWindow.setVisible(
true
);
18
}
19
}
This is the file
FirstSwingDemo.java
.
1
import
java.awt.event.ActionListener;
This is the file
EndingListener.java
.
2
import
java.awt.event.ActionEvent;
3
public class
EndingListener
implements
ActionListener
4{
5
public void
actionPerformed(ActionEvent e)
6 {
7 System.exit(0);
8 }
9}
(continued)