📄 rootpane.html
字号:
</div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Using Swing Components</a> </span> <div class=NavBit> <a target=_top href=progress.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=scrollpane.html>Next »</a> </div> <div id=PageTitle>How to Use Root Panes</div> <blockquote>In general, you don't directly create a <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRootPane.html"><code>JRootPane</code></a> object.Instead, you get a <code>JRootPane</code>(whether you want it or not!)when you instantiate <a href="internalframe.html"><code>JInternalFrame</code></a> orone of the top-level Swing containers,such as<a href="applet.html"><code>JApplet</code></a>,<a href="dialog.html"><code>JDialog</code></a>, and<a href="frame.html"><code>JFrame</code></a>.<p><a href="toplevel.html">Using Top-Level Containers</a>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 paneand how you can use them.Another place to get information about root panes is <a href="http://java.sun.com/products/jfc/tsc/index.html"><em>The Swing Connection</em></a>,especially the article<ahref="http://java.sun.com/products/jfc/tsc/articles/containers/index.html">Understanding Containers</a>.<p><center><IMG SRC="../../figures/uiswing/components/1layers.gif" WIDTH="370" HEIGHT="127" ALIGN="BOTTOM" ALT="A root pane manages four other panes: a layered pane, a menu bar, a content pane, and a glass pane."></center></p>As the preceding figure shows, a root pane has four parts: <dl><dt> <strong>The glass pane</strong><dd> Hidden, by default. If you make the glass pane visible, then it's like a sheet of glass over all the other parts of the root pane. It's completely transparent unless you implement the glass pane's <code>paintComponent</code> method so that it does something, and it intercepts input events for the root pane. In the <a href="#glasspane">next section</a>, you'll see an example of using a glass pane.<dt> <strong>The layered pane</strong><dd> Serves to position its contents, which consist of the content pane and the optional menu bar. Can also hold other components in a specified Z order. For information, see <a href="#layeredpane">The Layered Pane</a>.<dt> <strong>The content pane</strong><dd> The container of the root pane's visible components, excluding the menu bar. For information on using the content pane, see <a href="toplevel.html">Using Top-Level Containers</a>.<dt> <strong>The optional menu bar</strong><dd> The home for the root pane's container's menus. If the container has a menu bar, you generally use the container's <code>setJMenuBar</code> method to put the menu bar in the appropriate place. For more information on using menus and menu bars, see <a href="menu.html">How to Use Menus</a>.</dl></blockquote><h3><a name="glasspane">The Glass Pane</a></h3><blockquote>The glass pane is useful when you wantto be able to catch events or paintover an area that already contains one or more components.For example, you can deactivate mouse events for a multi-component regionby having the glass pane intercept the events.Or you can display an image over multiple componentsusing the glass pane.<p>Here's a picture of an applicationthat demonstrates glass pane features.It contains a check box that lets you setwhether 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.<p><p><center><IMG SRC="../../figures/uiswing/components/GlassPaneDemo.png" WIDTH="321" HEIGHT="93" ALIGN="BOTTOM" ALT="A snapshot of GlassPaneDemo"></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/GlassPaneDemo.jnlp">Run GlassPaneDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#GlassPaneDemo">example index</a>.<li> Click Button 1. <br> The button's appearance changes to show that it's been clicked.<li> Click the check box so that the glass pane becomes "visible," and then click Button 1 again. <br> 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.<li> Click the check box again so that the glass pane is hidden. <br> 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.</ol><hr></blockquote><p>The following code from <a class="SourceLink" target="_blank" href="examples/GlassPaneDemo.java"><code>GlassPaneDemo.java</code></a> 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 <code>getGlassPane</code>.<blockquote><pre>myGlassPane = new MyGlassPane(...);changeButton.addItemListener(myGlassPane);frame.setGlassPane(myGlassPane);...class MyGlassPane extends JComponent implements ItemListener { ... //React to change button clicks. public void itemStateChanged(ItemEvent e) { <b>setVisible(e.getStateChange() == ItemEvent.SELECTED);</b> }...}</pre></blockquote><p>The next code snippetimplements 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.<blockquote><pre><em>...//In the implementation of the glass pane's mouse listener:</em>public void mouseMoved(MouseEvent e) { redispatchMouseEvent(e, false);}<em>.../* The mouseDragged, mouseClicked, mouseEntered, * mouseExited, and mousePressed methods have the same * implementation as mouseMoved. */...</em>public void mouseReleased(MouseEvent e) { redispatchMouseEvent(e, true);}private void redispatchMouseEvent(MouseEvent e, boolean repaint) { Point glassPanePoint = e.getPoint(); Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint( glassPane, glassPanePoint, contentPane); if (containerPoint.y < 0) { //we're not in the content pane <em>//Could have special code to handle mouse events over //the menu bar or non-system window decorations, such as //the ones provided by the Java look and feel.</em> } else { //The mouse event is probably over the content pane. //Find out exactly which component it's over. Component component = SwingUtilities.getDeepestComponentAt( container, containerPoint.x, containerPoint.y); 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())); } } //Update the glass pane if requested. if (repaint) { glassPane.setPoint(glassPanePoint); glassPane.repaint(); }}</pre></blockquote>Here is the code in <code>MyGlassPane</code>that implements the painting.<blockquote><pre>protected void paintComponent(Graphics g) { if (point != null) { g.setColor(Color.red); g.fillOval(point.x - 10, point.y - 10, 20, 20); }}</pre></blockquote></blockquote><h3><a name="layeredpane">The Layered Pane</a></h3><blockquote>A layered pane isa container with depth such thatoverlapping components can appear one on top of the other.General information about layered panes is in<a class="TutorialLink" target="_top" href="layeredpane.html">How to Use Layered Panes</a>.This section discusses the particulars of how root panes use layered panes.<p>Each root pane places its menu bar and content panein an instance of <code>JLayeredPane</code>.The Z ordering that the layered pane providesenables behavior such asdisplaying popup menus above other components.<p>You can choose to put components in the root pane's layered pane.If you do, then you should be aware that certain depthsare 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 layersand their relationship:<p><center><IMG SRC="../../figures/uiswing/components/layeredPaneLayers.gif" WIDTH="239" HEIGHT="169" ALIGN="BOTTOM" ALT="Layers defined by JLayeredPane"></center></p>The table below describes the intended use for each layerand lists the <code>JLayeredPane</code> constantthat corresponds to each layer:<table cellpadding=5><tr><th align=left>Layer Name<th align=left>Value<th align=left>Description</th></tr><tr valign=top><td><code>FRAME_CONTENT_LAYER</code> <td> <code>new Integer(-30000)</code></td><td> The root pane adds the menu bar and content pane to its layered pane at this depth.</td></tr><tr valign=top><td><code>DEFAULT_LAYER</code> <td> <code>new Integer(0)</code></td><td> If you don't specify a component's depth when adding it to a layered pane, the layered pane puts it at this depth.</td></tr><tr valign=top><td><code>PALETTE_LAYER</code> <td> <code>new Integer(100)</code></td><td> This layer is useful for floating tool bars and palettes.</td></tr><tr valign=top><td><code>MODAL_LAYER</code> <td> <code>new Integer(200)</code></td><td> Modal internal-frame dialogs would belong in this layer.</td></tr><tr valign=top><td><code>POPUP_LAYER</code> <td> <code>new Integer(300)</code></td><td> Popups go in this layer because they need to appear above just about everything.</td></tr><tr valign=top><td><code>DRAG_LAYER</code> <td> <code>new Integer(400)</code></td><td> Intended to be used when a component is being dragged.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -