Java Reference
In-Depth Information
1
// Frame that closes on a window-close event
2
3
public class CloseableFrame extends JFrame
4
implements WindowListener
5
{
6
public CloseableFrame( )
7
{ addWindowListener( this ); }
8
9
public void windowClosing( WindowEvent event )
10
{ System.exit( 0 ); }
11
public void windowClosed( WindowEvent event )
12
{ }
13
public void windowDeiconified( WindowEvent event )
14
{ }
15
public void windowIconified( WindowEvent event )
16
{ }
17
public void windowActivated( WindowEvent event )
18
{ }
19
public void windowDeactivated( WindowEvent event )
20
{ }
21
public void windowOpened( WindowEvent event )
22
{ }
23
}
figure B.10
CloseableFrame
class:
same as
JFrame
but
handles the window-
closing event
figure B.11
main
routine for
Figure B.1
1
class BasicGUI extends CloseableFrame
2
{
3
public static void main( String [ ] args )
4
{
5
JFrame f = new BasicGUI( );
6
f.setTitle( "GUI Demo" );
7
8
Container contentPane = f.getContentPane( );
9
contentPane.add( new GUI( ) );
10
f.pack( );
11
f.show( );
12
}
13
}
anonymous inner classes
The
CloseableFrame
class is a mess. To listen for a
WindowEvent
, we must declare
a class that implements the
WindowListener
interface, instantiate the class, and
then register that object with the
CloseableFrame
. Since the
Window-Listener
Search WWH ::
Custom Search