Java Reference
In-Depth Information
reminder that, on Swing, you should just use
setDefaultCloseOperation()
. If you're writ-
ing an AWT-only application, you'll have to live with the deprecation warning.
Example 14-5. WindowCloser.java
package
package
com
.
darwinsys
.
swingui
;
import
import
java.awt.Window
java.awt.Window
;
import
import
java.awt.event.WindowAdapter
java.awt.event.WindowAdapter
;
import
import
java.awt.event.WindowEvent
java.awt.event.WindowEvent
;
/** A WindowCloser - watch for Window Closing events, and
* follow them up with setVisible(false), dispose(), and optionally
* ends (it all) with a System.exit(0).
* @deprecated For simple closing, just use JFrame.setDefaultCloseOperation().
*/
public
public class
class
WindowCloser
WindowCloser
extends
extends
WindowAdapter
{
/** The window we are to close */
Window win
;
/** True if we are to exit as well. */
boolean
boolean
doExit
=
false
false
;
/** Construct a WindowCloser that doesn't exit, just closes the window */
public
public
WindowCloser
(
Window w
) {
this
this
(
w
,
false
false
);
}
/** Construct a WindowCloser with control over whether it exits */
public
public
WindowCloser
(
Window w
,
boolean
boolean
exit
) {
win
=
w
;
doExit
=
exit
;
}
/** Called by AWT when the user tries to close the window */
public
public
void
void
windowClosing
(
WindowEvent e
) {
win
.
setVisible
(
false
false
);
win
.
dispose
();
iif
(
doExit
)
System
.
exit
(
0
);
}
}
Using it is straightforward: