How do I make my frames close in response to user requests?


You need to define an event handler for the "WindowClosing" event. Under JDK1.02, you can add the following code to your Frame subclasses.

// Response to close events
public void WindowClosing(WindowEvent evt)
	// Close application

If you're using JDK1.1 AWT event handlers, you can use the WindowAdapter class for a similar purpose. Here's a simple application that demonstrates the use of WindowAdapter, as an anonymous inner class.

import java.awt.*;
import java.awt.event.*;

public class Demo
   public static void main ( String args[] )
	// Create a frame with a button
	Frame f = new Frame("Demo");

	// Add a new button
	f.add ( new Button("demo") );

	// Resize/pack

	// Add a window listener
	f.addWindowListener ( new WindowAdapter () {
		public void windowClosing ( WindowEvent evt )

	// Show window

It doesn't take much code to respond to close events, and makes applications look much more professional. Don't forget in your next application! :)