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

📄 using.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
of every component within that container.One drawback of absolute positioningis that it doesn't adjust well when the top-level container is resized.It also doesn't adjust wellto differences between users and systems,such as different font sizes and<a class="TutorialLink" target="_top" href="../../i18n/locale/">locales</a>.</blockquote><h3><a name="add"> Adding Components to a Container </a></h3><blockquote>When you add components to a panel or content pane,the arguments you specify to the <code>add</code> methoddepend on the layout manager that the panel or content pane is using.For example, <code>BorderLayout</code>requires that you specify the areato which the component should be added,using code like this:<blockquote><pre>pane.add(aComponent, BorderLayout.PAGE_START);</pre></blockquote><p>The how-to sectionfor each layout managerhas details on what, if any,arguments you need to specify tothe <code>add</code> method.Some layout managers,such as <code>GridBagLayout</code>and <code>SpringLayout</code>,require elaborate setup procedures.Many layout managers, however,simply place componentsbased on the order they were added to their container.<p>Swing containersother than <code>JPanel</code> and content panesgenerally provide API that you should useinstead of the <code>add</code> method.For example, instead of adding a componentdirectly to a<a class="TutorialLink" target="_top" href="../components/scrollpane.html">scroll pane</a> (or, actually, to its viewport),you either specify the component in the <code>JScrollPane</code> constructoror use <code>setViewportView</code>.Because of specialized API like this,you don't need to knowwhich layout manager (if any) many Swing containers use.(For the curious:scroll panes happen to use a layout managernamed <code>ScrollPaneLayout</code>.)<p>For information about how to add components to a specific container,see the how-to page for the container.You can find the component how-to pages using<a class="TutorialLink" target="_top" href="../components/components.html">A Visual Index to the Swing Components</a>.</blockquote><h3><a name="sizealignment"> Providing Size and Alignment Hints </a></h3><blockquote>Sometimes you need to customize the size hintsthat a component provides to its container's layout manager,so that the component will be laid out well.You can do this by specifying one or more of theminimum, preferred, and maximum sizes of the component.You can invoke the component'smethods for setting size hints &#151;<code>setMinimumSize</code>,<code>setPreferredSize</code>,and <code>setMaximumSize</code>.Or you can create a subclass of the componentthat overrides the appropriate getter methods &#151;<code>getMinimumSize</code>,<code>getPreferredSize</code>,and <code>getMaximumSize</code>.Here is an example of making a component's maximum size unlimited:<blockquote><pre>component.setMaximumSize(new Dimension(Integer.MAX_VALUE,                                       Integer.MAX_VALUE));</pre></blockquote><p>Many layout managers don't pay attentionto a component's requested maximum size.However,<code>BoxLayout</code> and <code>SpringLayout</code> do.<p>Besides providing size hints,you can also provide alignment hints.For example,you can specify that the top edges of two components should be aligned.You set alignment hints either by invoking the component's<code>setAlignmentX</code> and<code>setAlignmentY</code> methods,or by overriding the component's<code>getAlignmentX</code> and<code>getAlignmentY</code> methods.Although most layout managers ignore alignment hints,<code>BoxLayout</code> honors them.You can find examples of setting the alignment in<a href="box.html">How to Use BoxLayout</a>.</blockquote><h3><a name="space">Putting Space Between Components</a></h3><blockquote>Three factors influence the amount of spacebetween visible components in a container:<dl><dt> The layout manager<dd> Some layout managers automatically put space     between components; others don't.     Some let you specify the amount of space between components.     See the how-to page for each layout manager     for information about spacing support.<dt> Invisible components<dd> You can create lightweight components     that perform no painting,     but that can take up space in the GUI.     Often, you use invisible components     in containers controlled by <code>BoxLayout</code>.     See <a href="box.html">How to Use BoxLayout</a>     for examples of using invisible components.<dt> Empty borders<dd> No matter what the layout manager,     you can affect the apparent amount of space     between components     by adding empty borders to components.     The best candidates for empty borders are     components that typically have no default border,     such as panels and labels.     Some other components might not work well with borders     in some look-and-feel implementations,     because of the way their painting code is implemented.     For information about borders, see<a class="TutorialLink" target="_top" href="../components/border.html">How to Use Borders</a>.</dl></blockquote><h3><a name="orientation">Setting the Container's Orientation</a></h3><blockquote>Thiswebsiteis written in English,with text that runs from left to right,and then top to bottom.However, many other languages havedifferent orientations.The <em>componentOrientation</em> propertyprovides a way of indicatingthat a particular componentshould use something differentfrom the defaultleft-to-right,top-to-bottom orientation.In a component such as a radio button,the orientation might be usedas a hintthat the look and feel shouldswitch the locations of the icon and text in the button.In a container,the orientation is usedas a hint to the layout manager.<p>To set a container's orientation, you can use eitherthe <code>Component</code>-defined method<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html#setComponentOrientation(java.awt.ComponentOrientation)"><code>setComponentOrientation</code></a> or,to set the orientation on the container's children as well,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html#applyComponentOrientation(java.awt.ComponentOrientation)"><code>applyComponentOrientation</code></a>.The argument to either methodcan be a constantsuch as <code>ComponentOrientation.RIGHT_TO_LEFT</code>,or it can be a call to the <code>ComponentOrientation</code> method<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/ComponentOrientation.html#getOrientation(java.util.Locale)"><code>getOrientation(Locale)</code></a>.  For example,the following code causes all<code>JComponent</code>sto be initialized with an Arabic-language locale,and then sets the orientationof the content pane and all components inside it accordingly:<blockquote><pre>JComponent.setDefaultLocale(new Locale("ar"));JFrame frame = new JFrame();...Container contentPane = frame.getContentPane();contentPane.applyComponentOrientation(    ComponentOrientation.getOrientation(        contentPane.getLocale()));</pre></blockquote>Here are two picturesshowing how <code>FlowLayout</code> lays out componentsin containersthat are exactly the same,except for their orientation.<p align=center><IMG SRC="../../figures/uiswing/layout/FlowLayoutDemo.png" WIDTH="471" HEIGHT="70" ALT="FlowLayoutDemo with default, left-to-right orientation"><br>Default orientation (left-to-right)</p><p align=center><IMG SRC="../../figures/uiswing/layout/FlowLayoutDemo-rtl.png" WIDTH="471" HEIGHT="70" ALT="FlowLayoutDemo with right-to-left orientation"><br>Right-to-left orientation</p>The standard layout managersthat support component orientationare <code>FlowLayout</code>,<code>BorderLayout</code>,<code>BoxLayout</code>,<code>GridBagLayout</code>, and<code>GridLayout</code>.</blockquote><h3><a name="choosing">Tips on Choosing a Layout Manager</a></h3><blockquote>Layout managershave different strengths and weaknesses.This section discusses some common layout scenariosand which layout managers might work for each scenario.If none of the layout managers we discuss is right for your situation,feel free to use other layout managersthat you write or find.Also keep in mind that flexible layout managerssuch as <code>GridBagLayout</code> and<code>SpringLayout</code>can fulfill many layout needs.<p><dl><dt><strong>Scenario:</strong>You need to display a component in as much space as it can get.<dd>If it's the only component in its container,use<a href="grid.html"><code>GridLayout</code></a> or<a href="border.html"><code>BorderLayout</code></a>.Otherwise,<code>BorderLayout</code> or<a href="gridbag.html"><code>GridBagLayout</code></a>might be a good match.<p>If you use <code>BorderLayout</code>,you'll need to put the space-hungry component in the center.With <code>GridBagLayout</code>,you'll need to set the constraints for the component so that<code>fill=GridBagConstraints.BOTH</code>.Another possibility is to use<a href="box.html"><code>BoxLayout</code></a>,making the space-hungry componentspecify very large preferred and maximum sizes.<p><dt><strong>Scenario:</strong>You need to display a few components in a compact rowat their natural size.<dd>Consider using a <code>JPanel</code> to group the componentsand using either the <code>JPanel</code>'s default<a href="flow.html"><code>FlowLayout</code></a> manageror the<a href="box.html"><code>BoxLayout</code></a> manager.<a href="spring.html"><code>SpringLayout</code></a>is also good for this.<p><dt><strong>Scenario:</strong>You need to display a few components of the same sizein rows and columns.<dd><a href="grid.html"><code>GridLayout</code></a> is perfect for this.<p><dt><strong>Scenario:</strong>You need to display a few components in a row or column,possibly with varying amounts of space between them,custom alignment, or custom component sizes.<dd><a href="box.html"><code>BoxLayout</code></a>is perfect for this.<p><dt><strong>Scenario:</strong>You need to display aligned columns,as in a form-like interfacewhere a column of labelsis used to describe text fields in an adjacent column.<dd><a href="spring.html"><code>SpringLayout</code></a>is a natural choice for this.The <code>SpringUtilities</code> classused by several Tutorial examplesdefines a <code>makeCompactGrid</code> methodthat lets you easily align multiple rows and columns of components.<p><dt><strong>Scenario:</strong>You have a complex layout with many components.<dd>Consider either usinga very flexible layout manager such as<a href="gridbag.html"><code>GridBagLayout</code></a> or<a href="spring.html"><code>SpringLayout</code></a>,or grouping the components into one or more <code>JPanel</code>sto simplify layout.If you take the latter approach,each <code>JPanel</code> might use a different layout manager.</dl></blockquote></blockquote><hr><font size=-1>*<a name="footnote">Way back in JDK 1.1</a>a second interface, <code>LayoutManager2</code>, was introduced.<code>LayoutManager2</code> extends<code>LayoutManager</code>,providing support for maximum size and alignment.Many layout managers don't use those features,however.</font><blockquote>        </blockquote>        <div class=NavBit>            <a target=_top href=visual.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=howLayoutWorks.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> A Visual Guide to Layout Managers        <br><b>Next page:</b> How Layout Management Works    </div>    </body></html> 

⌨️ 快捷键说明

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