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
System.exit(0);
}
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
f.pack();
// Add a window listener
f.addWindowListener ( new WindowAdapter () {
public void windowClosing ( WindowEvent evt )
{
System.exit(0);
}
});
// Show window
f.setVisible(true);
}
}
SOURCE : www.referjava.com
No comments:
Post a Comment