📄 flow.html
字号:
showLeft(document.getElementById("LeftBar").className == "LeftBar_hidden"); document.getElementById("ToggleLeft").blur(); } function load() { showLeft(leftBar()); document.getElementById("ToggleLeft").style.display="inline"; } </script> </head><body onload="load()"> <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl> <div id=TopBar_right> <a target="_blank" href="http://java.sun.com/javase/6/download.jsp">Download the JDK</a> <br> <a href="../../search.html" target="_blank">Search the Tutorials</a> <br> <a href="javascript:toggleLeft()" id="ToggleLeft">Hide the TOC</a> </div> </div> </div> </div> </div> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing <br><b>Lesson:</b> Laying Out Components Within a Container </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Laying Out Components Within a Container</a></div><div class="linkAHEAD"><a href="visual.html">A Visual Guide to Layout Managers</a></div><div class="linkAHEAD"><a href="using.html">Using Layout Managers</a></div><div class="linkAHEAD"><a href="howLayoutWorks.html">How Layout Management Works</a></div><div class="linkAHEAD"><a href="layoutlist.html">How to Use Various Layout Managers</a></div><div class="linkAHEAD"><a href="border.html">How to Use BorderLayout</a></div><div class="linkAHEAD"><a href="box.html">How to Use BoxLayout</a></div><div class="linkAHEAD"><a href="card.html">How to Use CardLayout</a></div><div class="nolinkAHEAD">How to Use FlowLayout</div><div class="linkAHEAD"><a href="gridbag.html">How to Use GridBagLayout</a></div><div class="linkAHEAD"><a href="grid.html">How to Use GridLayout</a></div><div class="linkAHEAD"><a href="group.html">How to Use GroupLayout</a></div><div class="linkBHEAD"><a href="groupExample.html">A GroupLayout Example</a></div><div class="linkAHEAD"><a href="spring.html">How to Use SpringLayout</a></div><div class="linkAHEAD"><a href="custom.html">Creating a Custom Layout Manager</a></div><div class="linkAHEAD"><a href="none.html">Doing Without a Layout Manager (Absolute Positioning)</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Layout 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>Laying Out Components Within a Container</a> </span> <div class=NavBit> <a target=_top href=card.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=gridbag.html>Next »</a> </div> <div id=PageTitle>How to Use FlowLayout</div> <blockquote>The <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/FlowLayout.html"><code>FlowLayout</code></a> class provides a very simple layout managerthat is used, by default, by <code>JPanel</code>s.Here's a picture of an example that uses a flow layout:<p><p><center><IMG SRC="../../figures/uiswing/layout/FlowLayoutDemo.png" WIDTH="471" HEIGHT="70" ALIGN="BOTTOM" ALT="A snapshot of FlowLayoutDemo"></center></p><p>You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/FlowLayoutDemo.jnlp">run FlowLayoutDemo</a>using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.Its code is in<a class="SourceLink" target="_blank" href="examples/FlowLayoutDemo.java"><code>FlowLayoutDemo.java</code></a>.<p><code>FlowLayout</code> puts components in a row,sized at their preferred size.If the horizontal space in the containeris too small to put all the components in one row,<code>FlowLayout</code> uses multiple rows.If the container is wider than necessary for a row of components,the row is, by default, centered horizontally within the container.You can specify that it stick to the left or right side insteadby using a <code>FlowLayout</code> constructorthat takes an alignment argument.You can also specify how much vertical or horizontal paddingis put around the components.<p>Below is the codefrom <code>FlowLayoutDemo.java</code>that creates the <code>FlowLayout</code> and the components it manages.<blockquote><pre>contentPane.setLayout(new FlowLayout());contentPane.add(new JButton("Button 1"));contentPane.add(new JButton("Button 2"));contentPane.add(new JButton("Button 3"));contentPane.add(new JButton("Long-Named Button 4"));contentPane.add(new JButton("5"));</pre></blockquote></blockquote><h3><a name="api">The FlowLayout API</a></h3><blockquote>[PENDING: This section will be converted to use API tables,as in the components section.]<p>The <code>FlowLayout</code> class has three constructors:<blockquote><pre>public FlowLayout()public FlowLayout(int <em>alignment</em>)public FlowLayout(int <em>alignment</em>, int <em>horizontalGap</em>, int <em>verticalGap</em>)</pre></blockquote>The <em><code>alignment</code></em> argument can be<code>FlowLayout.LEADING</code>,<code>FlowLayout.CENTER</code>, or<code>FlowLayout.TRAILING</code>.When the <code>FlowLayout</code> controlsa container with a left-to-right component orientation (the default),<code>LEADING</code> specifies that the components be left-alignedand <code>TRAILING</code> specifies right alignment.The <em><code>horizontalGap</code></em>and <em><code>verticalGap</code></em> argumentsspecify the number of pixels to put between components.If you don't specify a gap value,<code>FlowLayout</code>uses <code>5</code> for the default gap value.</blockquote><h3><a name="eg">Examples that Use FlowLayout</a></h3><blockquote>The following table lists some of the examples that use flow layout.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td valign=top> <a href="examples/index.html#FlowLayoutDemo"><code>FlowLayoutDemo</code></a></td><td valign=top> This page</td><td valign=top> Sets up a content pane to use <code>FlowLayout</code>. If you set the <code>RIGHT_TO_LEFT</code> constant to <code>true</code> and recompile, you can see how <code>FlowLayout</code> handles a container that has a right-to-left component orientation.</td></tr><tr><td valign=top> <a href="examples/index.html#CardLayoutDemo"><code>CardLayoutDemo</code></a></td><td valign=top> <a href="card.html">How to Use CardLayout</a></td><td valign=top> To center a component nicely in the top part of a <code>BorderLayout</code>, puts the component in a <code>JPanel</code> that uses a <code>FlowLayout</code>.</td></tr><tr><td valign=top> <a class="TutorialLink" target="_top" href="../components/examples/index.html#ButtonDemo"><code>ButtonDemo</code></a></td><td valign=top> <a class="TutorialLink" target="_top" href="../components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></td><td valign=top> Uses the default <code>FlowLayout</code> of a <code>JPanel</code>.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#TextInputDemo"><code>TextInputDemo</code></a></td><td valign=top><a class="TutorialLink" target="_top" href="../components/formattedtextfield.html">How to Use Formatted Text Fields</a></td><td valign=top> Uses a panelwith a right-aligned <code>FlowLayout</code>to present two buttons.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=card.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=gridbag.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 CardLayout <br><b>Next page:</b> How to Use GridBagLayout </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -