The JWindow Class
JWindow
is an
extension of java.awt.Window
that
uses a JRootPane
as its single component. Other than this core
distinction, JWindow
does not change
anything defined by the Window
class.
In AWT, one common reason for using the Window
class was to create a pop-up menu.
Since Swing explicitly provides a JPopupMenu
class (see Chapter 14), there is no need to extend
JWindow
for this purpose. The only
time you’ll use JWindow
is if you
have something that needs to be displayed in its own window without the
adornments added by JFrame
. Remember,
this means that the window can only be moved or closed programmatically
(or via the user’s platform-specific window manager controls, if
available).
One possible use for JWindow
would be to display a splash screen
when an application is starting up. Many programs display screens like
this, containing copyright information, resource loading status, etc.
Here’s such a program:
// SplashScreen.java // import java.awt.*; import javax.swing.*; public class SplashScreen extends JWindow { private int duration; public SplashScreen(int d) { duration = d; } // A simple little method to show a title screen in the center of the screen for // the amount of time given in the constructor public void showSplash( ) { JPanel content = (JPanel)getContentPane( ); content.setBackground(Color.white); // Set the window's bounds, centering the window. int width = 450; int height =115; Dimension screen = Toolkit.getDefaultToolkit( ).getScreenSize( ); int ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.