Java Reference
In-Depth Information
package
package com . darwinsys . swingui ;
import
import java.awt.Frame
java.awt.Frame ;
import
import java.awt.Label
java.awt.Label ;
import
import com.darwinsys.swingui.WindowCloser
com.darwinsys.swingui.WindowCloser ;
/* Example of closing a Window. */
public
public class
class WindowCloserDemo
WindowCloserDemo {
/* Main method */
public
public static
void main ( String [] argv ) {
Frame f = new
static void
new Frame ( "Close Me" );
f . add ( new
new Label ( "Try Titlebar Close" , Label . CENTER ));
f . setSize ( 100 , 100 );
f . setVisible ( true
true );
f . addWindowListener ( new
new WindowCloser ( f , true
true ));
}
}
Notice that some of this “quick and dirty” class extends Frame or JFrame directly. It is gener-
ally better to have a main program that creates a JFrame and installs the “main” GUI com-
ponent into that. This scheme promotes greater reusability. For example, if your graphing
program's main component extends JComponent , it can be added to a JPanel in another ap-
plication; whereas if it extends JFrame , it cannot.
See Also
I've mentioned dispose() several times without saying much about it. Inherited from Win-
dow , the dispose() method causes the underlying (operating system-specific) window sys-
tem resources to be released without totally destroying the Window . If you later call pack()
or setVisible(true) on the Window , the native resources are re-created. It's a good idea to
dispose() a window if you won't be using it for a while, but not if there's a good chance
you'll need it again soon.
In addition to WindowListener , Swing has several other multimethod interfaces, including
MouseListener and ComponentListener , and an Adapter class for each of these.
Search WWH ::




Custom Search