📄 group.html
字号:
.addComponent(c4));</pre></blockquote> <p>You can specify the alignment for parallel groups. It can be one of the following constants defined in the <code>GroupLayout.Alignment</code> enum: <code>LEADING</code>, <code>TRAILING</code>, <code>CENTER</code>, and <code>BASELINE</code>. These constants are used for both dimensions and depend on whether the componentorientation is left-to-right or right-to-left (top-to-bottom or bottom-to-top). For example, if the horizontal (vertical) component orientation is left-to-right (top-to-bottom) <code>LEADING</code> means left (top) while <code>TRAILING</code> means right (bottom). <code>CENTER</code> means "centered" in both dimensions. If you do not specify the alignment, <code>LEADING</code> will be used. The <code>BASELINE</code> alignment is valid only in the vertical dimension.<p><blockquote><hr><strong>Note:</strong> Alignment in the layout of a group only has meaning for components of different sizes. Components of the same size will be automatically aligned for each of the <code>GroupLayout.Alignment</code> constants.<hr></blockquote><p>Some comments about the code:<ul><li>You do not need to add the component directly to the container—that is done for you implicitly when using one of the addComponent methods.<p></li><li>Note the chained calls of the <code>addComponent</code> methods used to fill the groups. The <code>addComponent</code> method always returns the group on which it is called. Thanks to this you don't need to use local variables to hold the groups.<p></li><li>It is a good idea to indent the code so it is easy to see the hierarchical structure of the groups. Give each component a new line, add one level of indent for each new group in the hierarchy. A good source editor will help you with pairing the parenthesis to close the <code>createXXXGroup</code> methods. By following these simple rules, it is easier to add a new component or remove an existing one.<p></li></ul><p><h3>Component Size and Resizability</h3><p>There is no limit on the number of resizablecomponents in a layout.<p>The size of each component in a <code>GroupLayout</code> is constrained by three values;minimum size, preferred size and maximum size. These sizes control how thecomponent resizes within the layout. The <code>GroupLayout.addComponent(...)</code> method allows the sizeconstraints to be specified. <p>If not specified explicitly, the layout asks the component for its defaultsizes (by using the component's <code>getMinimumSize()</code>, <code>getPreferredSize()</code>and <code>getMaximumSize()</code> methods). You don't need tospecify anything for most of the components, like making <code>JTextField</code>resizable or <code>JButton</code> fixed, because the components themselves have the desired resizingbehavior as default. On the other hand you can override the default behavior.For example you can make a <code>JTextField</code> fixed or <code>JButton</code> resizable.<p><code>GroupLayout</code> defines constants that provide precise control overresize behavior. They can be used as parameters in the <code>addComponent(Component comp, int min, int pref, int max)</code> method. Here are two examples:<ol><li>To force a component to be resizable (allow shrinking and growing):</p><blockquote><pre><i>group</i>.addComponent(component, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ...</pre></blockquote><p>This allows the component to resize between zero size (minimum) to any size (<code>Short.MAX_VALUE</code> as maximum size means "infinite"). If we wanted the component not to shrink below its default minimum size, we'd use <code>GroupLayout.DEFAULT_SIZE</code> instead of <code>0</code> in the second parameter.</li><li>To make a component fixed size (suppress resizing):<blockquote><pre>group.addComponent(component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) ...</pre></blockquote></li></ol><p>In these examples the initial size of the component is not altered, itsdefault size is the component's preferred size. If wewanted a specific size for the component, we would specify it in the secondparameter instead of using <code>GroupLayout.DEFAULT_SIZE</code>.</p><p><b>Resizable gaps</b><p>Specifying size and resizability applies to gaps as well, including the preferred ones.For example, you can specify a preferred gapbetween two components that acts like a <i> spring</i> pushing the components awayfrom each other (to the opposite sides of the container). The preferred distanceof the two components is only used as the minimum size of the gap. See thefollowing snippet:<blockquote><pre>layout.createSequentialGroup() .addComponent(c1) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(c2);</pre></blockquote><p><h3>Sizing in Parallel Groups</h3><p>Resizable elements placed in a parallel group are stretched to fill thespace of the group determined by the largest element in the group, so they end upaligned with the same size. <code>GroupLayout</code> also provides control over whether the enclosing parallel group itself should resize. If group resizing is suppressed, it prevents the contained elements from growing over the preferred size of the group. This way you can make a block of components align on both sides, or constrain individual components to have the same size.<p>Let's try to achieve the same size for two components from our example (<code>c3</code> and <code>c4</code> in the horizontaldimension):<blockquote><pre>layout.createParallelGroup(GroupLayout.Alignment.LEADING, false) .addComponent(c3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(c4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);</pre></blockquote><p>The underlying mechanism works as follows:<ol><li>The size of the parallel group is set to the preferred size of the largest element; so to the preferred size of <code>c4</code> in our example.</li><P><li>Resizable elements are stretched to the size of the group. In our example, only <code>c3</code> is effectively stretched, the size of <code>c4</code> already corresponds to the size of the group.</li></ol><p>As a result, <code>c3</code> and <code>c4</code> would have the same width.The components would not resize further because the parallel group itself is notresizable (the second parameter of the <code>createParallelGroup</code> method, above, is <code>false</code>).<p><center><IMG SRC="../../figures/uiswing/layout/same_size_stretched.PNG" WIDTH="160" HEIGHT="62" ALIGN="BOTTOM" ALT="stretched."></center></p><p><p>Question for attentive readers: Why do we define both components in theparallel group as resizable in this example? It seems enough to have just <code>c3</code> resizable since <code>c4</code> is not stretched anyway...<p>The answer is: because of platform and localization independence.Otherwise we would have to rely on that <code>c4</code> component is always bigger than <code>c3</code>. But this may change when the application runs on different platform or is translated to another language. By having both components resizing they adjust to each other, no matter which one is bigger at the moment.<p><h3>Making Components the Same Size</h3><p>The previous case is special because the components are in the same parallelgroup. But what if we wanted unrelated components to have the same size?Clearly, the same size can't always be ensured by grouping. The OK and Cancel buttonsin a row at the bottom of a dialog are a good example. For this purpose <code>GroupLayout</code> provides a <code>linkSize</code> method. This method allowsthe size of arbitrary components to be linked regardless of where they are placed. The resulting size of the linked components is set according to the largest component. For example:<blockquote><pre>layout.linkSize(SwingConstants.HORIZONTAL, c3, c4);</pre></blockquote><p>In this example, the size is linked selectively for the horizontal dimension.<p><h3>Runtime Changes to Your GUI</h3><p>There are two important methods that you can use to make changes to your GUI at runtime, <code>replace()</code> and <code>setHonorsVisibility()</code>. Using these two methods, you can exchange components or change the visibility of components at runtime and have the GUI rearrange itself accordingly.<p><code>replace(Component existingComponent, Component newComponent)</code> replaces an existing component with a new one. One of the common operations needed for dynamic layouts is the ability to replace components like this. For example, perhaps a check box toggles between a component displaying a graph or a tree. <code>GroupLayout</code> makes this scenario simple with the <code>replace()</code> method. You can swap components without recreating all the groups.<p>Another common operation in user interfaces is to dynamically change the visibility of components.Perhaps components are shown only as a user completes earlier portions of a form. To avoid components shuffling around in such a scenario, space should be taken up regardless of the visibility of the components. <code>GroupLayout</code> offers two ways to configure how invisible components are treated. The <code>setHonorsVisibility(boolean)</code> method globally sets how invisible components are handled. A value of true, the default, indicates invisible components are treated as if they aren't there. Onthe other hand, a value of false provides space for invisible components, treating them as though they were visible. The <code>setHonorsVisibility(Component,Boolean)</code> method can be used to configure thebehavior at the level of a specific component. To determine how visibility is handled, <code>GroupLayout</code> first checks if a value has been specified for the Component, if not, it checks the setting of the global property.<p><p><blockquote><hr><strong>Some history:</strong> <code>GroupLayout</code> in the Java Standard Edition 6 consists of three distinct bodies of work: the ability to get the baseline for acomponent, the ability to get the preferred gap between components(<code>LayoutStyle</code>), and <code>GroupLayout</code>. This work was originally done as an open source project at <a class="OutsideLink" target="_blank" href="http://swing-layout.dev.java.net ">http://swing-layout.dev.java.net</a><p>NetBeans 5.0 supports <code>GroupLayout</code> by way of the swing-layout project. Because of thesuccess of this work, all three portions have been rolled into <code>GroupLayout</code> in JavaStandard Edition version 6. The main difference between the <code>GroupLayout</code>in Java SE 6 and swing-layout is in the package name and methodnames. NetBeans 5.5 provides the ability to target either the<code>GroupLayout</code> in Java SE 6, or the <code>GroupLayout</code> in swing-layout.Which version NetBeans targets is determined by the version of the Javaplatform your project targets. A project targeting Java SE 6 uses the<code>GroupLayout</code> in Java SE, otherwise <code>GroupLayout</code> in swing-layout is used.<hr></blockquote> </blockquote> <div class=NavBit> <a target=_top href=grid.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=groupExample.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 GridLayout <br><b>Next page:</b> A GroupLayout Example </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -