Java Reference
In-Depth Information
class of any other class such as WindowAdapter. In such cases, you make the class a
derived class of JFrame and have it implement the WindowListener interface. See
Self-Test Exercise 4 for an example.
Display 18.3
Using WindowAdapter
This requires the following import
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
1 private class CheckOnExit extends WindowAdapter
2 {
3 public void windowClosing(WindowEvent e)
4 {
5 ConfirmWindow checkers = new ConfirmWindow();
6 checkers.setVisible( true );
7 }
8 } //End of inner class CheckOnExit
If the definition of the inner class CheckOnExit in Display 18.2 were replaced with
this definition of CheckOnExit , there would be no difference in how the outer class
or any class behaves.
Self-Test Exercises
1. When you define a class and make it implement the WindowListener interface,
what methods must you define? What do you do if there is no particular action
that you want one of these methods to take?
2. The GUI in Display 18.2 has a main window. When the user clicks the
close-window button in the main window, a smaller window appears that says
"Are you sure you want to exit?" What happens if the user clicks the
close-window button in this smaller window? Explain your answer.
3. If you want a Swing program to end completely, you can invoke the method
System.exit. What if you want a JFrame window to go away, but you do not
want the program to end? What method can you have the JFrame invoke?
4. Rewrite the class in Display 18.2 so that the class is its own window listener.
Hint : The constructor will contain
addWindowListener( this );
 
 
Search WWH ::




Custom Search