I l@ve RuBoard |
![]() ![]() |
8.5 The JWindow ClassJWindow 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 x = (screen.width-width)/2; int y = (screen.height-height)/2; setBounds(x,y,width,height); // Build the splash screen. JLabel label = new JLabel(new ImageIcon("oreilly.gif")); JLabel copyrt = new JLabel ("Copyright 2002, O'Reilly & Associates", JLabel.CENTER); copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12)); content.add(label, BorderLayout.CENTER); content.add(copyrt, BorderLayout.SOUTH); Color oraRed = new Color(156, 20, 20, 255); content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); // Display it. setVisible(true); // Wait a little while, maybe while loading resources. try { Thread.sleep(duration); } catch (Exception e) {} setVisible(false); } public void showSplashAndExit( ) { showSplash( ); System.exit(0); } public static void main(String[] args) { // Throw a nice little title page up on the screen first. SplashScreen splash = new SplashScreen(10000); // Normally, we'd call splash.showSplash( ) and get on with the program. // But, since this is only a test... splash.showSplashAndExit( ); } } All this program does is create a JWindow containing a pair of labels and display it in the center of the screen. In a real application, the title screen might be displayed while various system resources are being loaded (consider using a ProgressMonitor in this case). When run, this example displays a simple window in the center of the screen, as shown in Figure 8-10. Figure 8-10. JWindow used as a splash screen![]() 8.5.1 PropertiesJWindow defines the properties shown in Table 8-9. The contentPane , glassPane, and layeredPane are really properties of JRootPane (described earlier in the chapter). Direct access is provided for convenience. Unlike JFrame (and JApplet, described below), JWindow does not provide direct access to the root pane's menu bar. This is just an indication of JWindow's intended usage. If you have some compelling reason to display a menu bar on a JWindow, you can always access it through the root pane or just add it as a component.
The layout property is listed here because JWindow overrides setLayout( ) to throw an Error if an attempt is made to change the layout manager, rather than set the layout manager of the window's content pane. The rootPane property is set to a new instance of JRootPane when the frame is created and cannot be changed using public methods. 8.5.2 Constructors
![]() |
I l@ve RuBoard |
![]() ![]() |