📄 layeredpane.html
字号:
to change a component's position.</blockquote><a name="layout"><h3>Laying Out Components in a Layered Pane</h3></a><blockquote>By default a layered pane has no layout manager.This means thatyou typically have to write the code that positions and sizes the componentsyou put in a layered pane.<p>The example uses the <code>setBounds</code> methodto set the size and position of each of the labels:<blockquote><pre>dukeLabel.setBounds(15, 225, icon.getIconWidth(), icon.getIconHeight());...label.setBounds(origin.x, origin.y, 140, 140);</pre></blockquote>When the user moves the mouse around,the program calls <code>setPosition</code> to change Duke's position:<blockquote><pre>dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);</pre></blockquote>Although a layered pane has no layout manager by default,you can still assign a layout manager to the layered pane.All of the layout managers provided by the Java platformarrange the components as if they were all on one layer.Here's a version of the previous demothat sets the layered pane's layout manager to an instanceof <code>GridLayout</code>,using that layout manager to lay out six colored labels.<p><center><IMG SRC="../../figures/uiswing/components/LayeredPaneDemo2Metal.png" WIDTH="306" HEIGHT="295" ALIGN="BOTTOM" ALT="A snapshot of LayeredPaneDemo2"></center></p>You can find the code for this program in<a class="SourceLink" target="_blank" href="examples/LayeredPaneDemo2.java"><code>LayeredPaneDemo2.java</code></a>.You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/LayeredPaneDemo2.jnlp">run LayeredPaneDemo2</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>. If you want to compile the example, consult the <a href="examples/index.html#LayeredPaneDemo2">example index</a> for a list of all necessary files.<p>Many programs use intermediate containers(such as panels) and their layout managersto lay out components on the same layer,but use absolute positioning to lay out components on different layers.For more information about absolute positioning, see<a class="TutorialLink" target="_top" href="../layout/none.html">Doing Without a Layout Manager (Absolute Positioning)</a>.</blockquote><h3><a name="api">The Layered Pane API</a></h3><blockquote>The following tables list the commonly used<code>JLayeredPane</code> constructors and methods.Other methods you are most likely to invoke ona <code>JLayeredPane</code> object are thoseit inherits from its superclasses,such as <code>setBorder</code>, <code>setPreferredSize</code>, and so on.See<a href="jcomponent.html#api">The JComponent API</a>for tables of commonly used inherited methods.<p>The API for using layered pane falls into these categories:<ul><li><a href="#creating">Creating or Getting a Layered Pane</a><li><a href="#layers">Layering Components</a><li><a href="#positions">Setting Component's Intra-Layer Positions</a></ul><p><table border=1><caption><a name="creating">Creating or Getting a Layered Pane</a></caption><tr><th align=left>Method or 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/JLayeredPane.html#JLayeredPane()">JLayeredPane()</a> </td> <td>Create a layered pane. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JApplet.html#getLayeredPane()">JLayeredPane getLayeredPane()</a> <br> <em>(in <code>JApplet</code>, <code>JDialog</code>, <code>JFrame</code>, and <code>JInternalFrame</code>) </td> <td>Get the automatic layered pane in an applet, dialog, frame, or internal frame. </td> </tr></table><p><table border=1><caption><a name="layers">Layering 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, 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> </td> <td>Add the specified component to the layered pane. The second argument, when present, is an <code>Integer</code> that indicates the layer. The third argument, when present, indicates the component's position within its layer. If you use the one-argument version of this method, the component is added to layer 0. If you use the one- or two-argument version of this method, the component is placed underneath all other components currently in the same layer. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#setLayer(java.awt.Component, int)">void setLayer(Component, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#setLayer(java.awt.Component, int, int)">void setLayer(Component, int, int)</a> </td> <td>Change the component's layer. The second argument indicates the layer. The third argument, when present, indicates the component's position within its layer. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#getLayer(java.awt.Component)">int getLayer(Component)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#getLayer(javax.swing.JComponent)">int getLayer(JComponent)</a> </td> <td>Get the layer for the specified component. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#getComponentCountInLayer(int)">int getComponentCountInLayer(int)</a> </td> <td>Get the number of components in the specified layer. The value returned by this method can be useful for computing position values. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#getComponentsInLayer(int)">Component[] getComponentsInLayer(int)</a> </td> <td>Get an array of all the components in the specified layer. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#highestLayer()">int highestLayer()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#lowestLayer()">int lowestLayer()</a> </td> <td>Compute the highest or lowest layer currently in use. </td> </tr></table><p><table border=1><caption><a name="positions">Setting Components' Intra-Layer Positions</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/javax/swing/JLayeredPane.html#setPosition(java.awt.Component, int)">void setPosition(Component, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#getPosition(java.awt.Component)">int getPosition(Component)</a> </td> <td>Set or get the position for the specified component within its layer. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#moveToFront(java.awt.Component)">void moveToFront(Component)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLayeredPane.html#moveToBack(java.awt.Component)">void moveToBack(Component)</a> </td> <td>Move the specified component to the front or back of its layer. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Layered Panes</a></h3><blockquote>This table shows the examples that use <code>JLayeredPane</code>and where those examples are described.<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#LayeredPaneDemo"><code>LayeredPaneDemo</code></a></td><td> This section</td><td> Illustrates layers and intra-layer positions of a <code>JLayeredPane</code>.</td></tr><tr><td> <a href="examples/index.html#LayeredPaneDemo2"><code>LayeredPaneDemo2</code></a></td><td> This section</td><td> Uses a layout manager to help lay out the components in a layered pane.</td></tr><tr><td> <a href="examples/index.html#RootLayeredPaneDemo"><code>RootLayeredPaneDemo</code></a></td><td> <a href="rootpane.html#examplemods">The Layered Pane</a></td><td> A version of <a href="examples/index.html#LayeredPaneDemo"> <code>LayeredPaneDemo</code></a> modified to use the root pane's layered pane.</td></tr><tr><td> <a href="examples/index.html#InternalFrameDemo"><code>InternalFrameDemo</code></a></td><td> <a href="internalframe.html">How to Use Internal Frames</a></td><td> Uses a <code>JDesktopFrame</code> to manage internal frames.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=label.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=list.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 Labels <br><b>Next page:</b> How to Use Lists </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -