public class GroupLayout extends Object implements LayoutManager2
GroupLayout是一个LayoutManager ,它可以对LayoutManager进行分层分组,以便将它们放置在一个Container 。 
       GroupLayout旨在供建筑商使用,但也可以手工编码。 
       分组是由Group类的实例完成的。 
       GroupLayout支持两种类型的组。 
       一个顺序组按顺序依次定位其子元素。 
       并行组以四种方式之一对齐其子元素。 
        每个组可包含任意数量的元素,其中的元素是Group , Component ,或间隙。 间隙可以被认为是具有最小,优选和最大尺寸的不可见成分。 另外GroupLayout支持优先缺口,其价值来自LayoutStyle 。 
 元素类似于春天。 每个元素具有由最小值,优选值和最大值指定的范围。 间隙具有开发者指定的范围,或由LayoutStyle的范围。 范围为Component S从确定Component的getMinimumSize , getPreferredSize和getMaximumSize方法。 此外,当添加Component s时,您可以指定要使用的特定范围,而不是组件的范围。 Group的范围由组的类型决定。 ParallelGroup的范围是其元素的范围的最大值。 SequentialGroup的范围是其元素范围的总和。 
 GroupLayout处理每个轴。 也就是说,存在表示水平轴的组和表示垂直轴的组。 水平组负责确定沿水平轴的最小尺寸,优选尺寸和最大尺寸,以及设置其中包含的组件的x和宽度。 垂直组负责确定沿垂直轴的最小尺寸,最小尺寸和最大尺寸,以及设置其中包含的组件的y和高度。 每个Component必须存在于水平和垂直组中,否则在布局期间抛出IllegalStateException ,或者要求最小,优选或最大大小时。 
下图显示沿水平轴的顺序组。 顺序组包含三个组件。 沿垂直轴使用平行组。

为了加强每个轴的独立处理,该图显示了每个轴和每个轴上的元件的范围。 每个组件的范围已投影到轴上,组将呈现为蓝色(水平)和红色(垂直)。 为了可读性,顺序组中的每个元素之间存在间隙。
沿着水平轴的顺序组呈现为实线蓝线。 注意顺序组是它包含的子元素的总和。
沿着垂直轴,平行组是每个部件的最大高度。 由于所有三个部件具有相同的高度,平行组具有相同的高度。
下图显示了相同的三个组件,但沿着水平轴的平行组和沿垂直轴的顺序组。

 由于c1是三大组件中最大的一个,并行组大小为c1 。 由于c2和c3小于c1它们基于为组件(如果指定)指定的对齐方式或并行组的默认对齐方式对齐。 在图c2和c3中创建的对齐方式为LEADING 。 如果组件方向是从右到左,则c2和c3将位于相对侧。 
下图显示了沿水平轴和垂直轴的顺序组。

 GroupLayout提供了在Component之间插入间隙的能力。 间隙的大小由LayoutStyle的实例LayoutStyle 。 这可以使用setAutoCreateGaps方法打开。 类似地,您可以使用setAutoCreateContainerGaps方法在触摸父容器和容器边缘的组件之间插入间隙。 
以下构建一个由一列中的两个标签组成的面板,后面是两列文本框:
  JComponent panel = ...;
   GroupLayout layout = new GroupLayout(panel);
   panel.setLayout(layout);
   // Turn on automatically adding gaps between components
   layout.setAutoCreateGaps(true);
   // Turn on automatically creating gaps between components that touch
   // the edge of the container and the container.
   layout.setAutoCreateContainerGaps(true);
   // Create a sequential group for the horizontal axis.
   GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
   // The sequential group in turn contains two parallel groups.
   // One parallel group contains the labels, the other the text fields.
   // Putting the labels in a parallel group along the horizontal axis
   // positions them at the same x location.
   //
   // Variable indentation is used to reinforce the level of grouping.
   hGroup.addGroup(layout.createParallelGroup().
            addComponent(label1).addComponent(label2));
   hGroup.addGroup(layout.createParallelGroup().
            addComponent(tf1).addComponent(tf2));
   layout.setHorizontalGroup(hGroup);
   // Create a sequential group for the vertical axis.
   GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
   // The sequential group contains two parallel groups that align
   // the contents along the baseline. The first parallel group contains
   // the first label and text field, and the second parallel group contains
   // the second label and text field. By using a sequential group
   // the labels and text fields are positioned vertically after one another.
   vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(label1).addComponent(tf1));
   vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(label2).addComponent(tf2));
   layout.setVerticalGroup(vGroup);  
       运行时产生以下内容。

此布局包括以下内容。
add方法Group 。 add方法返回调用者。 这样可以方便地链接调用。 例如group.addComponent(label1).addComponent(label2);相当于group.addComponent(label1); group.addComponent(label2); 。 Group秒; 而是使用create方法GroupLayout 。 | Modifier and Type | Class and Description | 
|---|---|
| static class  | GroupLayout.Alignment
              枚举可能的方式 
              ParallelGroup可以对齐其孩子。 | 
| class  | GroupLayout.GroupGroup提供了Group支持的两种操作的GroupLayout:一个接一个地布置组件(SequentialGroup)或对齐(ParallelGroup)。 | 
| class  | GroupLayout.ParallelGroup
              一个 
              Group对齐和大小的孩子。 | 
| class  | GroupLayout.SequentialGroup
              A 
              Group一个Group一个地Group定位和定尺寸其元素。 | 
| Modifier and Type | Field and Description | 
|---|---|
| static int | DEFAULT_SIZE
              表示组件或间隙的尺寸应用于特定范围值。 
             | 
| static int | PREFERRED_SIZE
              表示组件或间隙的首选尺寸应用于特定范围值。 
             | 
| Constructor and Description | 
|---|
| GroupLayout(Container host)
              创建一个 
              GroupLayout指定的Container。 | 
| Modifier and Type | Method and Description | 
|---|---|
| void | addLayoutComponent(Component component, Object constraints)
              该通知 
              Component已经被添加到父容器。 | 
| void | addLayoutComponent(String name, Component component)
              该通知 
              Component已经被添加到父容器。 | 
| GroupLayout.ParallelGroup | createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)
              创建并返回一个 
              ParallelGroup,使其沿着基线的元素对齐。 | 
| GroupLayout.ParallelGroup | createParallelGroup()
              创建并返回 
              ParallelGroup,对齐方式为Alignment.LEADING。 | 
| GroupLayout.ParallelGroup | createParallelGroup(GroupLayout.Alignment alignment)
              创建并返回一个 
              ParallelGroup使用指定的对齐。 | 
| GroupLayout.ParallelGroup | createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)
              创建并返回具有 
              ParallelGroup对齐和调整大小行为的ParallelGroup。 | 
| GroupLayout.SequentialGroup | createSequentialGroup()
              创建并返回一个 
              SequentialGroup。 | 
| boolean | getAutoCreateContainerGaps()
              返回 
              true如果容器和容器边界的组件之间的间隙自动创建。 | 
| boolean | getAutoCreateGaps()
              如果自动创建组件之间的间隙,则返回 
              true。 | 
| boolean | getHonorsVisibility()
              返回在确定组件的大小和位置时是否考虑组件可见性。 
             | 
| float | getLayoutAlignmentX(Container parent)
              返回沿x轴的对齐方式。 
             | 
| float | getLayoutAlignmentY(Container parent)
              返回沿着y轴的对齐。 
             | 
| LayoutStyle | getLayoutStyle()
              返回 
              LayoutStyle用于计算组件之间的间隙优选。 | 
| void | invalidateLayout(Container parent)
              使布局无效,指示如果布局管理器已缓存信息,则应将其丢弃。 
             | 
| void | layoutContainer(Container parent)
              放出指定的容器。 
             | 
| void | linkSize(Component... components)
              强制指定的组件具有相同的大小,无论其首选,最小或最大大小如何。 
             | 
| void | linkSize(int axis, Component... components)
              强制指定的组件沿指定的轴具有相同的大小,而不管其首选,最小或最大尺寸。 
             | 
| Dimension | maximumLayoutSize(Container parent)
              返回指定容器的最大大小。 
             | 
| Dimension | minimumLayoutSize(Container parent)
              返回指定容器的最小大小。 
             | 
| Dimension | preferredLayoutSize(Container parent)
              返回指定容器的首选大小。 
             | 
| void | removeLayoutComponent(Component component)
              通知指出 
              Component已从父容器中移除。 | 
| void | replace(Component existingComponent, Component newComponent)
              用新的组件代替现有组件。 
             | 
| void | setAutoCreateContainerGaps(boolean autoCreateContainerPadding)
              设置是否自动创建容器和组件之间接触容器边界的间隙。 
             | 
| void | setAutoCreateGaps(boolean autoCreatePadding)
              设置是否自动创建组件之间的间隙。 
             | 
| void | setHonorsVisibility(boolean honorsVisibility)
              设置在组件尺寸和定位时是否考虑组件可见性。 
             | 
| void | setHonorsVisibility(Component component, Boolean honorsVisibility)
              设置组件的可见性是否考虑到尺寸和定位。 
             | 
| void | setHorizontalGroup(GroupLayout.Group group)
              设置 
              Group位置和大小沿水平轴的组件。 | 
| void | setLayoutStyle(LayoutStyle layoutStyle)
              设置 
              LayoutStyle用于计算组件之间的首选间隙。 | 
| void | setVerticalGroup(GroupLayout.Group group)
              设置 
              Group位置和大小的沿垂直轴分量。 | 
| String | toString()
              返回此 
              GroupLayout的字符串表示GroupLayout。 | 
public static final int DEFAULT_SIZE
public static final int PREFERRED_SIZE
public GroupLayout(Container host)
GroupLayout指定的 
           Container 。 
          host - 
            Container的 
            GroupLayout是 
            LayoutManager为 
           IllegalArgumentException - 如果主机是 
            null 
           public void setHonorsVisibility(boolean honorsVisibility)
true表示非可见组件不应该作为布局的一部分来处理。 
           的值false表示组件应该被定位和无论能见度的尺寸。 
            当动态调整组件的可见性,并且不希望周围的组件和大小更改时,值false是有用的。 
指定的值用于没有指定显式可见性的组件。
 默认为true 。 
honorsVisibility - 在确定组件尺寸和定位时是否考虑组件可见性 
           setHonorsVisibility(Component,Boolean) 
           public boolean getHonorsVisibility()
public void setHonorsVisibility(Component component, Boolean honorsVisibility)
Boolean.TRUE表示如果component不可见,它不应该被作为布局的一部分处理。 
           的值false表示component定位和大小,而不管它的知名度。 
           值null setHonorsVisibility应该使用单参数方法null指定的值。 
            如果component不是Container这个GroupLayout的孩子正在管理,它将被添加到Container 。 
component - 组件 
           honorsVisibility - 这个 
            component是否应考虑到尺寸和定位 
           IllegalArgumentException - 如果 
            component是 
            null 
           setHonorsVisibility(Component,Boolean) 
           public void setAutoCreateGaps(boolean autoCreatePadding)
true ,并增加了两个组件到SequentialGroup自动创建的两个部件之间的间隙。 
           默认值为false 。 
          autoCreatePadding - 是否自动创建组件之间的间隙 
           public boolean getAutoCreateGaps()
true 。 
          true如果 
            true之间的间隙自动创建 
           public void setAutoCreateContainerGaps(boolean autoCreateContainerPadding)
false 。 
          autoCreateContainerPadding - 是否应自动创建容器和组件之间接触容器边界的间隙 
           public boolean getAutoCreateContainerGaps()
true 。 
          true如果容器和容器边界的组件之间的间隙自动创建 
           public void setHorizontalGroup(GroupLayout.Group group)
Group位置和大小沿水平轴的组件。 
          group - 
            Group ,它们沿水平轴位置和大小分量 
           IllegalArgumentException - 如果组是 
            null 
           public void setVerticalGroup(GroupLayout.Group group)
Group位置和尺寸沿垂直轴的部件。 
          group - 
            Group ,它们沿垂直轴位置和大小分量 
           IllegalArgumentException - 如果组是 
            null 
           public GroupLayout.SequentialGroup createSequentialGroup()
SequentialGroup 。 
          SequentialGroup 
           public GroupLayout.ParallelGroup createParallelGroup()
ParallelGroup ,对齐方式为Alignment.LEADING 。 
           这是一种更通用的createParallelGroup(Alignment)方法的覆盖方法。 
          ParallelGroup 
           createParallelGroup(Alignment) 
           public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment)
ParallelGroup使用指定的对齐。 
           这是为第二个参数提供的更通用的createParallelGroup(Alignment,boolean)方法的true的封面方法。 
          alignment - 组的元素的对齐 
           ParallelGroup 
           IllegalArgumentException - 如果 
            alignment是 
            null 
           createBaselineGroup(boolean, boolean) , GroupLayout.ParallelGroup 
           public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)
ParallelGroup对齐和调整大小行为的ParallelGroup。 
           alignment参数指定如何定位未填充组的子元素。 
           例如,如果一个ParallelGroup的对齐方式为TRAILING ,而一个孩子只需要50,那么孩子就位于位置50(从左到右的分量方向)。 
            基线对齐仅在沿垂直轴使用时有用。 用水平轴线的基线ParallelGroup创建的ParallelGroup被视为LEADING 。 
 有关基线组的行为的详细信息,请参阅ParallelGroup 。 
alignment - 组的元素的对齐 
           resizable - true如果组可调整大小; 
            如果组不可调整,则优选大小用于组的最小和最大大小 
           ParallelGroup 
           IllegalArgumentException - 如果 
            alignment是 
            null 
           createBaselineGroup(boolean, boolean) , GroupLayout.ParallelGroup 
           public GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)
ParallelGroup ,使其沿着基线的元素对齐。 
          resizable - 组是否可调整大小 
           anchorBaselineToTop - 基准是否固定在组的顶部或底部 
           createBaselineGroup(boolean, boolean) , GroupLayout.ParallelGroup 
           public void linkSize(Component... components)
这可以多次使用来强制任意数量的组件共享相同的大小。
链接的组件不可调整大小。
components - 
            Component s将具有相同的大小 
           IllegalArgumentException - 如果 
            components是 
            null ,或包含 
            null 
           linkSize(int,Component[]) 
           public void linkSize(int axis,
                     Component... components) 
          这可以多次使用来强制任意数量的组件共享相同的大小。
 链接Component不能调整大小。 
components - 那个 
            Component的大小相同 
           axis - 连接大小的轴; 
            SwingConstants.HORIZONTAL或SwingConstans.VERTICAL 
           IllegalArgumentException - 如果components是null ,或包含null ; 
            或axis不是SwingConstants.HORIZONTAL或SwingConstants.VERTICAL 
           public void replace(Component existingComponent, Component newComponent)
existingComponent - 应该删除并替换为 
            newComponent 
           newComponent - 组件放在 
            existingComponent的地方 
           IllegalArgumentException - 如果任何一个组件是 
            null或 
            existingComponent未被该布局管理器管理 
           public void setLayoutStyle(LayoutStyle layoutStyle)
LayoutStyle用于计算组件之间的首选间隙。 
           值null LayoutStyle应该使用共享的LayoutStyle实例。 
          layoutStyle - 
            LayoutStyle使用 
           LayoutStyle 
           public LayoutStyle getLayoutStyle()
LayoutStyle用于计算组件之间的间隙优选。 
           这将返回指定为setLayoutStyle的值,可能是null 。 
          LayoutStyle用于计算组件之间的优选间隙 
           public void addLayoutComponent(String name, Component component)
Component已经被添加到父容器。 
           您不应该直接调用此方法,而应该使用Group之一来添加一个Component 。 
          addLayoutComponent在接口 
            LayoutManager 
           name - 要与组件关联的字符串 
           component - 要加入的 
            Component 
           public void removeLayoutComponent(Component component)
Component已从父容器中删除。 
           你不应该直接调用此方法,而不是调用remove父Container 。 
          removeLayoutComponent在界面 
            LayoutManager 
           component - 要删除的组件 
           Component.remove(java.awt.MenuComponent) 
           public Dimension preferredLayoutSize(Container parent)
preferredLayoutSize在接口 
            LayoutManager 
           parent - 返回首选大小的容器 
           parent 
           IllegalArgumentException - 如果 
            parent不一样 
            Container这是创建与 
           IllegalStateException - 如果添加到此布局的任何组件不在水平和垂直组中 
           Container.getPreferredSize() 
           public Dimension minimumLayoutSize(Container parent)
minimumLayoutSize在接口 
            LayoutManager 
           parent - 返回大小的容器 
           parent 
           IllegalArgumentException - 如果 
            parent是不一样的 
            Container这是创建与 
           IllegalStateException - 如果添加到此布局中的任何组件不在水平和垂直组中 
           Container.getMinimumSize() 
           public void layoutContainer(Container parent)
layoutContainer在界面 
            LayoutManager 
           parent - 要布置的容器 
           IllegalStateException - 如果添加到此布局中的任何组件不在水平和垂直组中 
           public void addLayoutComponent(Component component, Object constraints)
Component已经被添加到父容器。 
           您不应该直接调用此方法,而应该使用Group之一来添加一个Component 。 
          addLayoutComponent在接口 
            LayoutManager2 
           component - 添加的组件 
           constraints - 说明放置组件的位置 
           public Dimension maximumLayoutSize(Container parent)
maximumLayoutSize在界面 
            LayoutManager2 
           parent - 返回尺寸的容器 
           parent 
           IllegalArgumentException - 如果 
            parent是不一样的 
            Container这是创建与 
           IllegalStateException - 如果添加到此布局的任何组件不在水平和垂直组中 
           Container.getMaximumSize() 
           public float getLayoutAlignmentX(Container parent)
getLayoutAlignmentX在界面 
            LayoutManager2 
           parent - 
            Container托管这个 
            LayoutManager 
           .5 
           IllegalArgumentException - 如果 
            parent不一样 
            Container这是创建与 
           public float getLayoutAlignmentY(Container parent)
getLayoutAlignmentY在接口 
            LayoutManager2 
           parent - 
            Container托管这 
            LayoutManager 
           .5 
           IllegalArgumentException - 如果 
            parent是不一样的 
            Container这是创建与 
           public void invalidateLayout(Container parent)
invalidateLayout在接口 
            LayoutManager2 
           parent -在 
            Container承办此次布局管理 
           IllegalArgumentException - 如果 
            parent是不一样的 
            Container这是创建与 
            Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
 Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.