📄 panel.html
字号:
<ul><li> One <code>JPanel</code> instance — colored <b><font color="#ff0000">red</font></b> in the preceding snapshot — serves as a content pane for the application's frame. This content pane uses a top-to-bottom <a class="TutorialLink" target="_top" href="../layout/box.html"><code>BoxLayout</code></a> to lay out its contents, and an empty border to put 5 pixels of space around them. See <a href="toplevel.html">Using Top-Level Containers</a> for information about content panes.<li> Two instances of a custom <code>JPanel</code> subclass named <code>ConversionPanel</code> — colored <b><font color="#00ffff">cyan</font></b> — are used to both contain components and coordinate communication between components. The panels also have titled borders, which describe their contents and paint a line around them. Each panel uses a left-to-right <code>BoxLayout</code> object to lay out its contents.<li> In each <code>ConversionPanel</code>, a <code>JPanel</code> instance — colored <b><font color="#ff00ff">magenta</font></b> — is used to make a combo box be at the right size and position. Each panel uses a top-to-bottom <code>BoxLayout</code> object (helped by an invisible space-filling component) to lay out the combo box.<li> In each <code>ConversionPanel</code>, an instance of an unnamed <code>JPanel</code> subclass — colored <b><font color=#0000ff>blue</font></b> — groups two components (a text field and a slider) and restricts their size. Each panel uses a top-to-bottom <code>BoxLayout</code> object to lay out its contents.</ul>Here is what Converter normally looks like:<p><center><IMG SRC="../../figures/uiswing/components/ConverterMetal.png" WIDTH="288" HEIGHT="198" ALIGN="BOTTOM" ALT="Normal Converter"></center></p><p>As Converter demonstrates, panels are useful forgrouping components,simplifying component layout,and putting borders around groups of components.The rest of this section gives hints ongrouping and laying out components.For information about using borders, see<a class="TutorialLink" target="_top" href="../components/border.html">How to Use Borders</a>.</blockquote><h3><a name="layout">Setting the Layout Manager</a></h3><blockquote>Like other containers, a panel uses a layout manager to position and size its components.By default, a panel's layout manageris an instance of <a class="TutorialLink" target="_top" href="../layout/flow.html"><code>FlowLayout</code></a>,which places the panel's contents in a row.You can easily make a panel use any other layout managerby invoking the <code>setLayout</code> methodor specifying a layout manager when creating the panel.The latter approach is preferable for performance reasons,since it avoids the unnecessary creationof a <code>FlowLayout</code> object.<p>Here is an example of setting the layout managerwhen creating the panel:<blockquote><pre>JPanel p = new JPanel(new BorderLayout()); //PREFERRED!</pre></blockquote><p>This approach doesn't work with <code>BoxLayout</code>,since the <code>BoxLayout</code> constructor requiresa pre-existing container.Here is an example that uses <code>BoxLayout</code>:<blockquote><pre>JPanel p = new JPanel();p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));</pre></blockquote><p></blockquote><h3><a name="add">Adding Components</a></h3><blockquote>When you add components to a panel, you use the <code>add</code> method.Exactly which arguments you specify to the <code>add</code> methoddepend on which layout manager the panel uses.When the layout manager is <code>FlowLayout</code>, <code>BoxLayout</code>, <code>GridLayout</code>,or <code>SpringLayout</code>,you'll typically use the one-argument <code>add</code> method, like this:<blockquote><pre>aFlowPanel.add(aComponent);aFlowPanel.add(anotherComponent);</pre></blockquote>When the layout manager is <code>BorderLayout</code>,you need to provide an argumentspecifying the added component's position within the panel.For example:<blockquote><pre>aBorderPanel.add(aComponent, BorderLayout.CENTER);aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);</pre></blockquote>With <code>GridBagLayout</code>you can use either <code>add</code> method,but you must somehow specify <a class="TutorialLink" target="_top" href="../layout/gridbag.html#gridbagConstraints">grid bag constraints</a> for each component.<p>For information about choosing and using the standard layout managers, see<a class="TutorialLink" target="_top" href="../layout/using.html">Using Layout Managers</a>.</blockquote><h3><a name="api">The Panel API</a></h3><blockquote>The API in the <code>JPanel</code> class itself is minimal.The methods you are most likely to invoke ona <code>JPanel</code> object are those it inherits from its superclasses —<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html"><code>JComponent</code></a>, <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html"><code>Container</code></a>, and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html"><code>Component</code></a>.The following tables list the APIyou're most likely to use,with the exception of methodsrelated to<a class="TutorialLink" target="_top" href="../components/border.html">borders</a> and <a href=jcomponent.html#layoutapi>layout hints</a>.For more information about the API all <code>JComponent</code>scan use, see<a href="jcomponent.html">The JComponent Class</a>.<p><ul><li><a href="#creating">Creating a <code>JPanel</code></a><li><a href="#contents">Managing a Container's Components</a><li><a href="#layoutapi">Setting/Getting the Layout Manager</a></ul><table border=1><caption><a name="creating">Creating a <code>JPanel</code></a></caption><tr><th align=left>Constructor</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPanel.html#JPanel()">JPanel()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPanel.html#JPanel(java.awt.LayoutManager)">JPanel(LayoutManager)</a> </td> <td>Create a panel. The <code>LayoutManager</code> parameter provides a layout manager for the new panel. By default, a panel uses a <code>FlowLayout</code> to lay out its components. </td> </tr></table><p><table border=1> <caption><a name="contents">Managing a Container's Components</a></caption><tr><th align=left>Method</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component)">void add(Component)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component, int)">void add(Component, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component, java.lang.Object)">void add(Component, Object)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component, java.lang.Object, int)">void add(Component, Object, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.lang.String, java.awt.Component)">void add(String, Component)</a> </td> <td>Add the specified component to the panel. When present, the <code>int</code> parameter is the index of the component within the container. By default, the first component added is at index 0, the second is at index 1, and so on. The <code>Object</code> parameter is layout manager dependent and typically provides information to the layout manager regarding positioning and other layout constraints for the added component. The <code>String</code> parameter is similar to the <code>Object</code> parameter. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponentCount()">int getComponentCount()</a> </td> <td>Get the number of components in this panel. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponent(int)">Component getComponent(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponentAt(int, int)">Component getComponentAt(int, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponentAt(java.awt.Point)">Component getComponentAt(Point)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponents()">Component[] getComponents()</a> </td> <td>Get the specified component or components. You can get a component based on its index or <em>x, y</em> position. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#remove(java.awt.Component)">void remove(Component)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#remove(int)">void remove(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#removeAll()">void removeAll()</a> </td> <td>Remove the specified component(s). </td> </tr></table><p><table border=1><caption><a name="layoutapi">Setting/Getting the Layout Manager</a></caption><tr><th align=left>Method</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#setLayout(java.awt.LayoutManager)">void setLayout(LayoutManager)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getLayout()">LayoutManager getLayout()</a> </td> <td>Set or get the layout manager for this panel. The layout manager is responsible for positioning the panel's components within the panel's bounds according to some philosophy. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Panels</a></h3><blockquote>Many examples contained in this lesson use <code>JPanel</code> objects.The following table lists a few.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="examples/index.html#Converter"><code>Converter</code></a></td><td> This section</font></td><td> Uses five panels, four of which use <code>BoxLayout</code> and one of which uses <code>GridLayout</code>. The panels use borders and, as necessary, size and alignment hints to affect layout.</td></tr><tr><td> <a href="examples/index.html#ListDemo"><code>ListDemo</code></a></td><td> <a href="tabbedpane.html">How to Use Lists</a></td><td> Uses a panel, with its default <code>FlowLayout</code> manager, to center three components in a row.</td></tr><tr><td> <a href="examples/index.html#ToolBarDemo"><code>ToolBarDemo</code></a></td><td> <a href="toolbar.html">How to Use Toolbar</a></td><td> Uses a panel as a content pane. The panel contains three components, laid out by <code>BorderLayout</code>.</td></tr><tr><td> <a href="../misc/examples/index.html#BorderDemo"><code>BorderDemo</code></a></td><td> <a href="../components/border.html">How to Use Borders</a></td><td> Contains many panels that have various kinds of borders. Several panels use <code>BoxLayout</code>.</td></tr><tr><td> <a href="../layout/examples/index.html#BoxLayoutDemo"><code>BoxLayoutDemo</code></a></td><td> <a href="../layout/box.html">How to Use BoxLayout</a></td><td> Illustrates the use of a panel with Swing's <code>BoxLayout</code> manager.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=menu.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=passwordfield.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> How to Use Menus <br><b>Next page:</b> How to Use Password Fields </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -