📄 border.html
字号:
<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="nolinkAHEAD">How to Use BorderLayout</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="linkAHEAD"><a href="flow.html">How to Use FlowLayout</a></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=layoutlist.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=box.html>Next »</a> </div> <div id=PageTitle>How to Use BorderLayout</div> <blockquote>Here's a snapshot of an application that uses a<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/BorderLayout.html"><code>BorderLayout</code></a>.<p><p><center><IMG SRC="../../figures/uiswing/layout/BorderLayoutDemo.png" WIDTH="482" HEIGHT="186" ALIGN="BOTTOM" ALT="A snapshot of BorderLayoutDemo"></center></p>You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/BorderLayoutDemo.jnlp">run BorderLayoutDemo</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/BorderLayoutDemo.java"><code>BorderLayoutDemo.java</code></a>.<p>As the preceding picture shows,a <code>BorderLayout</code> has five areas.These areas are specified by the <code>BorderLayout</code> constants<code>PAGE_START</code>,<code>PAGE_END</code>,<code>LINE_START</code>,<code>LINE_END</code>, and<code>CENTER</code>.<blockquote><hr><strong>Version note:</strong> Before 1.4, the preferred names for the various areas were different,ranging from points of the compass(for example, <code>BorderLayout.NORTH</code>for the top area)to wordier versions of the constants we use in our examples.The constants our examples use are preferredbecause they are standard and enable programs to adjustto languages that have different orientations.<hr></blockquote><p>If you enlarge the window,the center areagets as much of the available space as possible.The other areas expand only as much as necessaryto fill all available space.Often, a container uses only one or two of theareas of the <code>BorderLayout</code> —just the center,or center and bottom, for example.<p>The following code adds components to a frame's content pane.Because content panes use <code>BorderLayout</code> by default,the code doesn't need to set the layout manager.You can find the whole program in<a class="SourceLink" target="_blank" href="examples/BorderLayoutDemo.java"><code>BorderLayoutDemo.java</code></a>.<blockquote><pre>...<em>//Container pane = aFrame.getContentPane()</em>...JButton button = new JButton("Button 1 (PAGE_START)");pane.add(button, BorderLayout.PAGE_START);//Make the center component big, since that's the//typical usage of BorderLayout.button = new JButton("Button 2 (CENTER)");button.setPreferredSize(new Dimension(200, 100));pane.add(button, BorderLayout.CENTER);button = new JButton("Button 3 (LINE_START)");pane.add(button, BorderLayout.LINE_START);button = new JButton("Long-Named Button 4 (PAGE_END)");pane.add(button, BorderLayout.PAGE_END);button = new JButton("5 (LINE_END)");pane.add(button, BorderLayout.LINE_END);</pre></blockquote><p>We strongly recommend that you specify the component's location(for example, <code>BorderLayout.LINE_END</code>)as one of the arguments to the <code>add</code> method.If you leave it out, the component will be added to the center,but your code will be much less clear.If you find that a component is missingfrom a container controlled by a <code>BorderLayout</code>,make sure that you specified the component's locationand that you didn't put another componentin the same location.<p>All our examples that use <code>BorderLayout</code>specify the component as the first argument to the<code>add</code> method.For example:<blockquote><pre>add(component, BorderLayout.CENTER) //preferred</pre></blockquote>However, you might see code in other programsthat specifies the component second.For example, here are alternate waysof writing the preceding code:<blockquote><pre>add(BorderLayout.CENTER, component) //valid but old fashioned <em>or</em>add("Center", component) //valid but error prone</pre></blockquote></blockquote><h3><a name="api">The BorderLayout API</a></h3><blockquote><code>BorderLayout</code> defines a couple of constructorsand some methods for adding space between components.<p>[PENDING: The following will be converted to be in the Tutorial'sstandard API table format.]<p>By default, a <code>BorderLayout</code>puts no gap between the components it manages.In the preceding applet, any apparent gaps are the resultof the buttonsreserving extra space around their apparent display area.You can specify gaps (in pixels) using the following constructor:<blockquote><pre>BorderLayout(int <em>horizontalGap</em>, int <em>verticalGap</em>)</pre></blockquote>You can also use the following methods to setthe horizontal and vertical gaps, respectively:<blockquote><pre>void setHgap(int)void setVgap(int)</pre></blockquote></blockquote><h3><a name="eg">Examples that Use BorderLayout</a></h3><blockquote>Here are a few of the manyexamples that use <code>BorderLayout</code>.<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#BorderLayoutDemo"><code>BorderLayoutDemo</code></a></td><td> This page</td><td> Puts a component in each of the five possible locations.</td></tr><tr><td> <a href="../components/examples/index.html#TabbedPaneDemo"><code>TabbedPaneDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/tabbedpane.html">How to Use Tabbed Panes</a></td><td> One of many examples that puts a single component in the center of a content pane, so that the component is as large as possible.</td></tr><tr><td> <a href="../components/examples/index.html#CheckBoxDemo"><code>CheckBoxDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/button.html#checkbox">How to Use Check Boxes</a></td><td> Creates a <code>JPanel</code> that uses a <code>BorderLayout</code>. Puts components into the left (actually, <code>LINE_START</code>) and center locations.</td></tr></table></blockquote> </blockquote> <div class=NavBit> <a target=_top href=layoutlist.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=box.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 Various Layout Managers <br><b>Next page:</b> How to Use BoxLayout </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -