Академический Документы
Профессиональный Документы
Культура Документы
A browser with JavaScript enabled is required for this page to operate properly.
Documentation
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take
advantage of improvements introduced in later releases.
Using Top-Level Containers tells you the basics of using root panes — getting the content pane, setting its
layout manager, and adding Swing components to it. This section tells you more about root panes, including
the components that make up a root pane and how you can use them.
1 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
The container of the root pane's visible components, excluding the menu bar. For information on
using the content pane, see Using Top-Level Containers.
The optional menu bar
The home for the root pane's container's menus. If the container has a menu bar, you generally use
the container's setJMenuBar method to put the menu bar in the appropriate place. For more
information on using menus and menu bars, see How to Use Menus.
The glass pane is useful when you want to be able to catch events or paint over an area that already
contains one or more components. For example, you can deactivate mouse events for a multi-component
region by having the glass pane intercept the events. Or you can display an image over multiple components
using the glass pane.
Here's a picture of an application that demonstrates glass pane features. It contains a check box that lets you
set whether the glass pane is "visible" — whether it can get events and paint itself onscreen. When the glass
pane is visible, it blocks all input events from reaching the components in the content pane. It also paints a
red dot in the place where it last detected a mouse-pressed event.
Try this:
1. Click the Launch button to run GlassPaneDemo using Java™ Web Start (download JDK 7
or later). Alternatively, to compile and run the example yourself, consult the example index.
2. Click Button 1.
The button's appearance changes to show that it's been clicked.
3. Click the check box so that the glass pane becomes "visible," and then click Button 1
again.
The button does not act clicked because the glass pane intercepts all the mouse events.
The glass pane paints a red circle where you release the mouse.
4. Click the check box again so that the glass pane is hidden.
When the glass pane detects an event over the check box, it forwards it to the check box.
Otherwise, the check box would not respond to clicks.
The following code from GlassPaneDemo.java shows and hides the glass pane. This program happens to
create its own glass pane. However, if a glass pane doesn't do any painting, the program might simply attach
listeners to the default glass pane, as returned by getGlassPane.
2 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
...
class MyGlassPane extends JComponent
implements ItemListener {
...
//React to change button clicks.
public void itemStateChanged(ItemEvent e) {
setVisible(e.getStateChange() == ItemEvent.SELECTED);
}
...
}
The next code snippet implements the mouse-event handling for the glass pane. If a mouse event occurs
over the check box, then the glass pane redispatches the event so that the check box receives it.
3 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
if ((component != null)
&& (component.equals(liveButton))) {
//Forward events over the check box.
Point componentPoint = SwingUtilities.convertPoint(
glassPane,
glassPanePoint,
component);
component.dispatchEvent(new MouseEvent(component,
e.getID(),
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
}
A layered pane is a container with depth such that overlapping components can appear one on top of the
other. General information about layered panes is in How to Use Layered Panes. This section discusses the
particulars of how root panes use layered panes.
Each root pane places its menu bar and content pane in an instance of JLayeredPane. The Z ordering that
the layered pane provides enables behavior such as displaying popup menus above other components.
You can choose to put components in the root pane's layered pane. If you do, then you should be aware that
certain depths are defined to be used for specific functions, and you should use the depths as intended.
Otherwise, your components might not play well with the others. Here's a diagram that shows the functional
layers and their relationship:
4 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
The table below describes the intended use for each layer and lists the JLayeredPane constant that
corresponds to each layer:
FRAME_CONTENT_LAYER new The root pane adds the menu bar and content pane to
Integer(-30000) its layered pane at this depth.
DEFAULT_LAYER new Integer(0) If you don't specify a component's depth when adding
it to a layered pane, the layered pane puts it at this
depth.
PALETTE_LAYER new Integer(100) This layer is useful for floating tool bars and palettes.
POPUP_LAYER new Integer(300) Popups go in this layer because they need to appear
above just about everything.
Here is a picture of RootLayeredPaneDemo, which is a version of LayeredPaneDemo that uses the root
pane's layered pane, rather than creating a new layered pane.
5 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
Try this:
1. Click the Launch button to run RootLayeredPaneDemo using Java™ Web Start (download
JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example
index.
2. Move the cursor around in the window, so that Duke moves on top of other components.
Note that when the cursor is on top of non-label components — whether it's in the content
pane or in the Java-look-and-feel provided title bar — Duke's movement is temporarily
stopped. This is because mouse-motion events go to the component that's deepest in the
containment hierarchy and is interested in mouse events. The mouse-motion listener that
moves Duke is registered on the layered pane, and most of the components in that pane
(with the exception of the labels) happen to have mouse-motion listeners. When the mouse
moves over an interested component in the layered pane, the layered pane doesn't get the
event and the interested component does.
3. Making sure the Top Position in Layer check box is selected, change Duke's layer to Yellow
(-30000).
As before, he appears on top of other components, except for the Magenta (0) and Cyan
(301) rectangles.
4. Keeping Duke in the Yellow layer, click the check box to send Duke to the back of layer
-30000.
Duke disappears because the content pane and all the components in it are now above
him.
5. Change Duke's layer to Cyan (301), move Duke down a bit so he's standing on the top
edge of the Yellow rectangle, and then press Space to bring up the combo box's drop-down
list.
If the look and feel implements the drop-down list as a lightweight popup, Duke appears on
6 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
The tables that follow list the API for using root panes, glass panes, and content panes. For more information
on using content panes, go to Using Top-Level Containers. Here are the tables in this section:
The API for using other parts of the root pane is described elsewhere:
JRootPane getRootPane()
Get the root pane of the applet, dialog, frame, internal frame, or
(in JApplet, JDialog, JFrame,
window.
JInternalFrame, and JWindow)
static JRootPane If the component contains a root pane, return that root pane.
getRootPane(Component) Otherwise, return the root pane (if any) that contains the
(in SwingUtilities) component.
Set or get which button (if any) is the default button in the root
void setDefaultButton(JButton)
pane. A look-and-feel-specific action, such as pressing Enter,
JButton getDefaultButton()
causes the button's action to be performed.
Setting or Getting the Root Pane's Contents
The following methods are defined in JApplet, JDialog, JFrame, JInternalFrame, JRootPane, and
JWindow, unless noted otherwise.
Method Purpose
void setGlassPane(Component)
Set or get the glass pane.
Component getGlassPane()
void setLayeredPane(JLayeredPane)
Set or get the layered pane.
Container getLayeredPane()
void setContentPane(Container)
Set or get the content pane.
Container getContentPane()
void setJMenuBar(JMenuBar)
JMenuBar getJMenuBar() Set or get the menu bar.
(not defined in JWindow)
7 of 8 2/1/19, 1:29 AM
How to Use Root Panes (The Java™ Tutorials > C... https://docs.oracle.com/javase/tutorial/uiswing/c...
Every Swing program has a root pane, but few reference it directly. The examples in the following list
illustrate how to use features of JRootPane or the glass pane. Also see these lists:
About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights
Copyright © 1995, 2017 Oracle and/or its affiliates. All rights reserved.
8 of 8 2/1/19, 1:29 AM