[ Back | Previous | Next ]

Using a Windowsadapter to control the events on a window

Package:
java.awt.event.*
Product:
JDK
Release:
1.1.x
Related Links:
ActionEvent
KeyEvent
MouseEvent
WindowEvent
Comment:

Using an Windowsadapter to control the events on a window container.

The window adapter is part of the java.awt.event classes. Once again for structual programming and neatly source these adapters are very nice to use. The main advantage is that you can instantly adapt the listener to an event on an individual level. Also you have more windowsadapters, like windowActivated(WindowEvent), windowClosed(WindowEvent), windowClosing(WindowEvent), windowDeactivated(WindowEvent) windowDeiconified(WindowEvent) windowIconified(WindowEvent) windowOpened(WindowEvent)

The initial event is brought by the windowListener attached to the container. In our example we have implemented an windowClosing() event with the windows adapter. event listener ... adapter class ...

import akaan.pacman.*;
import java.applet.Applet; 
import java.awt.event.*;
import java.awt.*;
class GUI extends Frame {       // App.GUI is enclosed in the PacManApp 
        Label info; // accessible for status info
        public GUI () {
                setLayout( new BorderLayout () );
                setBackground( Color.black ); 
                Panel northpanel = new Panel();
                ....
                ....
                addWindowListener( new AppAdapter() );  

addKeyListener( new MovementAdapter() ); addFocusListener( new PlayFocusAdapter() ); // pacman keycontrol }

// // WINDOW HANDLING // class AppAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { stop(); setVisible( false ); dispose(); System.exit(0); } } } // GUI class

1