Java Reference
In-Depth Information
Display 18.2
A Window Listener
(part 1 of 3)
1
import
javax.swing.JFrame;
2
import
javax.swing.JPanel;
3
import
java.awt.BorderLayout;
4
import
java.awt.FlowLayout;
5
import
java.awt.Color;
6
import
javax.swing.JLabel;
7
import
javax.swing.JButton;
8
import
java.awt.event.ActionListener;
9
import
java.awt.event.ActionEvent;
10
import
java.awt.event.WindowListener;
11
import
java.awt.event.WindowEvent;
12
public class
WindowListenerDemo
extends
JFrame
13 {
14
public static final int
WIDTH = 300;
//for main window
15
public static final int
HEIGHT = 200;
//for main window
16
public static final int
SMALL_WIDTH = 200;
//for confirm window
17
public static final int
SMALL_HEIGHT = 100;
//for confirm window
18
private class
CheckOnExit
implements
WindowListener
19 {
20
public void
windowOpened(WindowEvent e)
21 {}
This
WindowListener
class is an inner class.
22
public void
windowClosing(WindowEvent e)
23 {
24 ConfirmWindow checkers =
new
ConfirmWindow();
25 checkers.setVisible(
true
);
26 }
27
public void
windowClosed(WindowEvent e)
28 {}
29
public void
windowIconified(WindowEvent e)
30 {}
31
public void
windowDeiconified(WindowEvent e)
32 {}
A window listener must
define all the method
headings in the
W
indowListener
interface, even if some are trivial
implementations.
33
public void
windowActivated(WindowEvent e)
34 {}
35
public void
windowDeactivated(WindowEvent e)
36 {}
37 }
//End of inner class CheckOnExit
38
private class
ConfirmWindow
extends
JFrame
implements
ActionListener
39 {
40
public
ConfirmWindow()
41 {
42 setSize(SMALL_WIDTH, SMALL_HEIGHT);
43 getContentPane().setBackground(Color.YELLOW);
Another
(continued)