⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 box.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 3 页
字号:
by invoking the <code>setAlignmentY</code> methodor by overriding <code>getAlignmentY</code>.<p>Here is an example, taken from an application called<a href="examples/index.html#BoxAlignmentDemo"><code>BoxAlignmentDemo</code></a>,of changing the Y alignments of two buttonsso that the buttons' bottoms are aligned:<blockquote><pre>button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);</pre></blockquote>By default, most components have center X and Y alignment.However, buttons, combo boxes, labels, and menu itemshave a different default X alignment value:<code>LEFT_ALIGNMENT</code>.The previous picture shows what happensif you put a left-aligned component such as a labeltogether with a center-aligned componentin a container controlled by a top-to-bottom <code>BoxLayout</code>.<p>The <a href="examples/index.html#BoxAlignmentDemo"><code>BoxAlignmentDemo</code></a>program gives examples of fixing mismatched alignment problems.Usually, it's as simpleas making an offending button or labelbe center aligned.For example:<blockquote><pre>label.setAlignmentX(Component.CENTER_ALIGNMENT);</pre></blockquote><p></blockquote><h3><a name="size">Specifying Component Sizes</a></h3><blockquote><p>As we mentioned before,<code>BoxLayout</code> pays attentionto a component's requested minimum, preferred, and maximum sizes.While you're fine tuning the layout,you might need to adjust these sizes.<p>Sometimes the need to adjust the size is obvious.For example, a button's maximum sizeis generally the same as its preferred size.If you want the buttonto be drawn widerwhen additional space is available,then you need to change its maximum size.<p>Sometimes, however, the need to adjust size is not so obvious.You might be getting unexpected results with a box layout,and you might not know why.In this case, it's usually bestto treat the problem as an<a href="#alignment">alignment problem</a> first.If adjusting the alignments doesn't help,then you might have a size problem.We'll discuss this further a bit later.<blockquote><hr><strong>Note:</strong>&nbsp;Although <code>BoxLayout</code> pays attention toa component's maximum size,many layout managers do not.For example, if you put a buttonin the bottom part of a <code>BorderLayout</code>,the button will probably be wider than its preferred width,no matter what the button's maximum size is.<code>BoxLayout</code>, on the other hand,never makes a button wider than its maximum size.<hr></blockquote><p>You can change the minimum, preferred, and maximum sizesin two ways:<ul><li> By invoking the appropriate <code>set<em>Xxx</em>Size</code> method     (which is defined by the <code>JComponent</code> class).     For example:<blockquote><pre>comp.setMinimumSize(new Dimension(50, 25));comp.setPreferredSize(new Dimension(50, 25));comp.setMaximumSize(new Dimension(Short.MAX_VALUE,                                  Short.MAX_VALUE));</pre></blockquote><li> By overriding the appropriate <code>get<em>Xxx</em>Size</code> method.     For example:<blockquote><pre><em>...//in a subclass of a component class:</em>public Dimension getMaximumSize() {    size = getPreferredSize();    size.width = Short.MAX_VALUE;    return size;}</pre></blockquote></ul><p><p>If you're running into trouble with a box layoutand you've ruled out alignment problems,then the trouble might well be size-related.For example, if the container controlled by the box layoutis taking up too much space,then one or more of the components in the containerprobably needs to have its maximum size restricted.<p>You can use two techniques to track down size troublein a box layout:<ul><li> Add a garish line <a href="border.html">border</a>     to the outside of the Swing components in question.     This lets you see what size they really are.     For example:<blockquote><pre>comp.setBorder(BorderFactory.createCompoundBorder(                   BorderFactory.createLineBorder(Color.red),                   comp.getBorder()));</pre></blockquote><li> Use good old <code>System.out.println</code>     to print the components' minimum, preferred, and maximum     sizes, and perhaps their bounds.</ul></blockquote><h3><a name="api">The Box Layout API</a></h3><blockquote>[PENDING: We will make the left columns of the following tableshave links to the API doc.]<p>The following tables list the commonly used<code>BoxLayout</code> and<code>Box</code> constructors and methods.The API for using box layouts falls into these categories:<p><ul><li><a href="#construct">Creating <code>BoxLayout</code> objects</a><li><a href="#fillerapi">Creating space fillers</a><li><a href="#other">Other useful methods</a></ul><p><table border=1><caption><a name="construct">Creating <code>BoxLayout</code> Objects</a></caption><tr><th>Constructor or Method</th><th>Purpose</th></tr><tr><td><font size=2><code>BoxLayout(Container, int)</code></font></td><td>Creates a <code>BoxLayout</code> instance    that controls the specified <code>Container</code>.    The integer argument specifies the axis    along which the container's components    should be laid out.    When the container has the default component orientation,    <code>BoxLayout.LINE_AXIS</code>    specifies that the components be laid out    from left to right,    and <code>BoxLayout.PAGE_AXIS</code>    specifies that the components be laid out    from top to bottom.</td></tr></tr><tr><td><font size=2><code>Box(int)</code></font></td><td>Creates a <code>Box</code> &#151; a lightweight container    that uses a <code>BoxLayout</code>    with the specified axis.    As of release 1.3,    <code>Box</code> extends <code>JComponent</code>.    Before that,    it was implemented as a subclass of <code>Container</code>.</td></tr></tr><tr><td><font size=2><code>static Box createHorizontalBox()</font><br><em>(in <code>Box</code>)</em></td><td>Creates a <code>Box</code> that lays out its components    from left to right.</td></tr></tr><tr><td><font size=2><code>static Box createVerticalBox()</code></font><br><em>(in <code>Box</code>)</em></td><td>Creates a <code>Box</code> that lays out its components    from top to bottom.</td></tr></tr></table><p><table border=1><caption><a name="fillerapi">Creating Space Fillers</a><br><em>These methods are defined in the <code>Box</code> class.</em></caption><tr><th>Constructor or Method</th><th>Purpose</th></tr><tr><td><font size=2><code>Component createRigidArea(Dimension)</code></font></td><td>Create a <a href="#rigidarea">rigid</a> lightweight component.</td></tr><tr><td><font size=2><code>Component createHorizontalGlue()    <br>    Component createVerticalGlue()    <br>    Component createGlue()</code></font></td><td>Create a <a href="#glue">glue</a> lightweight component.    Horizontal glue and vertical glue can be very useful.</td></tr></tr><tr><td><font size=2><code>Component createHorizontalStrut()    <br>    Component createVerticalStrut()</code></font></td><td>Create a "strut" lightweight component.    We recommend using rigid areas    instead of struts.</td></tr></tr><tr><td><font size=2><code>Box.Filler(Dimension, Dimension,    Dimension)</code></font></td><td>Creates a lightweight component with the specified    minimum, preferred, and maximum sizes    (with the arguments specified in that order).    See the    <a href="#box.filler">custom <code>Box.Filler</code></a>    discussion,    earlier in this section, for details.</td></tr></tr></table><p><table border=1><caption><a name="other">Other Useful Methods</a></caption><tr><th>Method</th><th>Purpose</th></tr><tr><td><font size=2><code>void changeShape(Dimension, Dimension,    Dimension)</code> <em>(in <code>Box.Filler</code>)</em></font></td><td>Change the minimum, preferred, and maximum sizes    of the recipient <code>Box.Filler</code> object.    The layout changes accordingly.</td></tr></tr></table></blockquote><h3><a name="eg">Examples that Use Box Layouts</a></h3><blockquote>The following table lists some of the many examplesthat use box layouts.<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#BoxLayoutDemo2">BoxLayoutDemo2</a></td><td valign=top> This page</td><td valign=top> Uses a box layout to create a centered column of components.</td></tr><tr><td valign=top> <a href="examples/index.html#BoxAlignmentDemo">BoxAlignmentDemo</a></td><td valign=top> This page</td><td valign=top> Demonstrates how to fix common alignment problems.</td></tr><tr><td valign=top> <a href="examples/index.html#BoxLayoutDemo">BoxLayoutDemo</a></td><td valign=top> This page</td><td valign=top> Lets you play with alignments and maximum sizes.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#ListDialog">ListDialog</a></td><td valign=top> This page</td><td valign=top> A simple yet realistic example     of using both a top-to-bottom box layout     and a left-to-right one.     Uses horizontal glue, rigid areas, and empty borders.     Also sets the X alignment of a component.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../events/examples/index.html#InternalFrameEventDemo">InternalFrameEventDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../events/internalframelistener.html">How to Write an Internal Frame Listener</a></td><td valign=top> Uses a top-to-bottom layout to center     buttons and a scroll pane     in an internal frame.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#MenuGlueDemo">MenuGlueDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../components/menu.html#custom">Customizing Menu Layout</a></td><td valign=top> Shows how to right-align a menu in the menu bar,     using a glue component.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#MenuLayoutDemo">MenuLayoutDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../components/menu.html#custom">Customizing Menu Layout</a></td><td valign=top> Shows how to customize menu layout     by changing the menu bar to use a top-to-bottom box layout,     and the popup menu to use a left-to-right box layout.</td></tr><tr><td valign=top><code>ConversionPanel.java</code> in the<a class="TutorialLink" target="_top" href="../components/examples/index.html#Converter">Converter</a> demo</td><td valign=top><a class="TutorialLink" target="_top" href="../components/panel.html">How to Use Panels</a></td><td valign=top> Aligns two components in different box-layout-controlled containers     by setting the components' widths to be the same,     and their containers' widths to be the same.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=border.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=card.html>Next &raquo;</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 BorderLayout        <br><b>Next page:</b> How to Use CardLayout    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -