📄 toplevel.html
字号:
<div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div> </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=html.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=model.html>Next »</a> </div> <div id=PageTitle>Using Top-Level Containers</div> <blockquote>As we mentioned before,Swing provides three generally useful top-level container classes: <a href="frame.html"><code>JFrame</code></a>, <a href="dialog.html"><code>JDialog</code></a>, and <a href="applet.html"><code>JApplet</code></a>.When using these classes,you should keep these facts in mind:<ul><li> To appear onscreen, every GUI component must be part of a <em>containment hierarchy</em>. A containment hierarchy is a tree of components that has a top-level container as its root. We'll show you one in a bit.<p><li> Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.<p><li> Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI.<p><li> You can optionally add a menu bar to a top-level container. The menu bar is by convention positioned within the top-level container, but outside the content pane. Some look and feels, such as the Mac OS look and feel, give you the option of placing the menu bar in another place more appropriate for the look and feel, such as at the top of the screen.</ul><a name="windownote"><blockquote><hr><strong>Note:</strong> Although <a href="internalframe.html"><code>JInternalFrame</code></a>mimics <code>JFrame</code>,internal frames aren't actually top-level containers.<hr></blockquote></a>Here's a picture of a frame created by an application.The frame contains a green menu bar (with no menus)and, in the frame's content pane, a large blank, yellow label. <p><table><tr><td><p><center><IMG SRC="../../figures/uiswing/components/TopLevelDemoMetal.png" WIDTH="208" HEIGHT="234" ALIGN="BOTTOM" ALT="A simple application with a frame that contains a menu bar and a content pane."></center></p></td><td><p><center><IMG SRC="../../figures/uiswing/components/ConceptualPane.gif" WIDTH="264" HEIGHT="164" ALIGN="BOTTOM" ALT="A diagram of the frame's major parts"></center></p></td></tr></table><p>You can find the entire source for this example in<a class="SourceLink" target="_blank" href="examples/TopLevelDemo.java"><code>TopLevelDemo.java</code></a>.Although the example uses a <code>JFrame</code> ina standalone application, the same concepts applyto <code>JApplet</code>s and <code>JDialog</code>s.<p>Here's the containment hierarchy for this example's GUI:<p><center><IMG SRC="../../figures/uiswing/components/3jframe.gif" WIDTH="189" HEIGHT="143" ALIGN="BOTTOM" ALT="Containment hierarchy for the TopLeveDemo example's GUI."></center></p><p>As the ellipses imply,we left some details out of this diagram.We reveal the missing details a bit later.Here are the topics this section discusses:<ul><li> <a href="#general">Top-Level Containers and Containment Hierarchies</a><li> <a href="#contentpane">Adding Components to the Content Pane</a><li> <a href="#menubar">Adding a Menu Bar</a><li> <a href="#rootpane">The Root Pane (a.k.a. The Missing Details)</a></ul><a name="general"></blockquote><h3>Top-Level Containers and Containment Hierarchies</h3></a><blockquote>Each program that uses Swing components has at least onetop-level container.This top-level container is the root of a containment hierarchy —the hierarchy that contains all of the Swing components that appear inside the top-level container.<p>As a rule, a standalone application with a Swing-based GUIhas at least one containmenthierarchy with a <code>JFrame</code> as its root.For example, if an application has one main window and two dialogs,then the application has three containment hierarchies,and thus three top-level containers.One containment hierarchy has a <code>JFrame</code>as its root,and each of the other two has a <code>JDialog</code> objectas its root.<p>A Swing-based applet has at least one containment hierarchy,exactly one of which is rooted by a <code>JApplet</code> object.For example, an applet that brings up a dialoghas two containment hierarchies.The components in the browser windoware in a containment hierarchyrooted by a <code>JApplet</code> object.The dialog has a containment hierarchy rooted by a <code>JDialog</code> object.</blockquote><a name="contentpane"><h3>Adding Components to the Content Pane</h3></a><blockquote>Here's the code that the preceding exampleuses to get a frame's content pane and add the yellowlabel to it:<blockquote><pre>frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);</pre></blockquote>As the code shows,you find the content pane of a top-level containerby calling the <code>getContentPane</code> method.The default content pane is a simple intermediate container that inherits from <code>JComponent</code>,and that uses a <code>BorderLayout</code> as its layout manager.<p>It's easy to customize the content pane —setting the layout manageror adding a border, for example.However, there is one tiny gotcha.The <code>getContentPane</code> methodreturns a <code>Container</code> object,not a <code>JComponent</code> object.This means that if you wantto take advantage of the content pane's <code>JComponent</code> features,you need to either typecast the return valueor create your own component to be the content pane.Our examples generally take the second approach,since it's a little cleaner.Another approach we sometimes take is to simply add a customized componentto the content pane,covering the content pane completely.<p>If you create your own content pane, make sure it's opaque.An opaque <code>JPanel</code> objectmakes a good content pane.Note that the default layout manager for <code>JPanel</code> is <code>FlowLayout</code>;you'll probably want to change it.<p>To make a component the content pane,use the top-level container's <code>setContentPane</code> method.For example:<blockquote><pre>//Create a panel and add components to it.JPanel contentPane = new JPanel(new BorderLayout());contentPane.setBorder(<em>someBorder</em>);contentPane.add(<em>someComponent</em>, BorderLayout.CENTER);contentPane.add(<em>anotherComponent</em>, BorderLayout.PAGE_END);//Make it the content pane.contentPane.setOpaque(true);<em>topLevelContainer</em>.setContentPane(contentPane);</pre></blockquote> <blockquote><hr><strong>Note:</strong> Don't use non-opaque containers such as <code>JScrollPane</code>, <code>JSplitPane</code>, and <code>JTabbedPane</code> as content panes. A non-opaque content pane results in messy repaints. Although you can make any Swing component opaque by invoking <code>setOpaque(true)</code> on it, some components don't look right when they're completely opaque. For example, tabbed panes generally let part of the underlying container show through, so that the tabs look non-rectangular. An opaque tabbed pane just tends to look bad. <p> In most look and feels, <code>JPanel</code>s are opaque by default. However, <code>JPanel</code>s in the GTK+ look and feel are not initially opaque. To be safe, we invoke <code>setOpaque</code> on all <code>JPanel</code>s used as content panes. <hr></blockquote></blockquote><a name="menubar"><h3>Adding a Menu Bar</h3></a><blockquote>All top-level containers can, in theory, have a menu bar.In practice, however, menu bars usually appear only in framesand perhaps in applets.To add a menu bar to a top-level container,you create a <code>JMenuBar</code> object,populate it with menus,and then call <code>setJMenuBar</code>.The <code>TopLevelDemo</code> adds a menu barto its frame with this code:<blockquote><pre>frame.setJMenuBar(greenMenuBar);</pre></blockquote>For more information about implementing menusand menu bars, see <a href="menu.html">How to Use Menus</a>.</blockquote><a name="rootpane"><h3>The Root Pane</h3></a><blockquote>Each top-level container relies on a reclusive intermediate containercalled the <em>root pane</em>.The root pane manages the content pane and the menu bar,along with a couple of other containers.You generally don't need to know about root panesto use Swing components.However, if you ever need to intercept mouse clicksor paint over multiple components,you should get acquainted with root panes.<p>Here's a glimpse at the components that a root pane providesto a frame (and to every other top-level container):<p><center><IMG SRC="../../figures/uiswing/../ui/ui-rootPane.gif" WIDTH="367" HEIGHT="166" 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>We've already told you about the content pane and the optional menu bar.The two other components that a root pane adds are a layered pane and a glass pane.The layered pane directly contains the menu bar and content pane,and enables Z-ordering of other components you might add.The glass pane is often used to intercept input eventsoccuring over the top-level container,and can also be used to paint over multiple components.<p>For more information about the intricacies of root panes, see <a href="rootpane.html">How to Use Root Panes</a>.<p> </blockquote> <div class=NavBit> <a target=_top href=html.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=model.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> Using HTML in Swing Components <br><b>Next page:</b> Using Models </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -