GroupLayout

来源:互联网 发布:linux配置网络yum源 编辑:程序博客网 时间:2024/04/28 20:36
Implements: LayoutManager2
Inner classes: GroupLayout.Alignment, GroupLayout.Group, GroupLayout.SequentialGroup, GroupLayout.ParallelGroup

zh_cnGroupLayout 是一个 LayoutManager,它将组件按层次分组,以决定它们在 Container 中的位置。GroupLayout 主要供生成器使用,但也可以手工编码。分组由 Group 类的实例来完成。GroupLayout 支持两种组。串行组 (sequential group) 按顺序一个接一个地放置其子元素。并行组 (parallel group) 能够以四种方式对齐其子元素。

每个组可以包含任意数量的元素,其中元素有 GroupComponent 或间隙 (gap)。间隙可被视为一个具有最小大小、首选大小和最大大小的不可见组件。此外,GroupLayout 还支持其值取自 LayoutStyle 的首选间隙。

元素类似于一个弹簧。每个元素都有一个范围,由最小大小、首选大小和最大大小指定。间隙的范围由开发人员指定,或者由 LayoutStyle 确定。Component 的范围通过 Component 的 getMinimumSizegetPreferredSize 和 getMaximumSize 方法确定。此外,添加 Component 时,可以指定使用一个特定范围,而不使用该组件的范围。Group 的范围由组的类型确定。ParallelGroup 的范围是其元素范围的最大值。SequentialGroup 的范围是其元素范围的总和。

GroupLayout 将每个轴 (axis) 单独对待。也就是说,存在一个表示水平轴的组和一个表示垂直轴的组。水平组负责确定沿水平轴的最小大小、首选大小和最大大小,并设置所包含组件的 x 和宽度。垂直组负责确定沿垂直轴的最小大小、首选大小和最大大小,并设置所包含组件的 y 和高度。每个 Component都必须同时存在于水平组和垂直组中,否则,在布局过程中或者在请求最小大小、首选大小或最大大小时,将抛出 IllegalStateException

下图显示了一个沿水平轴的串行组。该串行组包含三个组件。沿垂直轴使用了一个并行组。

为了强调要单独对待每个轴,该图显示了沿每个轴的每个组和元素的范围。每个组件的范围已被投射到轴上,两个组分别呈现为蓝色(水平)和红色(垂直)。为了便于理解,串行组中的每个元素之间都有一个间隙。

沿水平轴的串行组呈现为蓝色实线。注意,串行组是它所包含的子元素的总和。

沿垂直轴,并行组是每个组件高度的最大值。由于三个组件的高度都相同,所以并行组具有相同的高度。

下图显示了与上图中相同的三个组件,但并行组沿水平轴,串行组沿垂直轴。

由于 c1 在三个组件中最大,所以并行组的大小被确定为 c1。由于 c2 和 c3 小于 c1,所以将根据为组件指定的对齐方式(如果已指定)或并行组的默认对齐方式将它们对齐。在该图中,c2 和 c3 是使用 LEADING 的对齐方式创建的。如果组件的方向是从右到左,那么 c2 和 c3 的位置将在相反的一侧对齐(沿右侧对齐)。

下图显示了沿水平和垂直两个轴的串行组。

GroupLayout 提供在 Component 之间插入间隙的能力。间隙的大小由 LayoutStyle 的实例确定。可以使用 setAutoCreateGaps 方法进行此操作。类似地,可以使用 setAutoCreateContainerGaps 方法在触到父容器边缘的组件和容器之间插入间隙。

以下代码构建了一个面板,该面板由两列构成,第一列中有两个标签,第二列中有两个文本字段:

JComponent panel = ...;GroupLayout layout = new GroupLayout(panel);panel.setLayout(layout); // Turn on automatically adding gaps between componentslayout.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); 

运行代码时,将出现下面的窗口。

此布局由以下部分组成。

  • 水平轴由一个包含两个并行组的串行组组成。第一个并行组包含标签,第二个并行组包含文本字段。
  • 垂直轴由一个包含两个并行组的串行组组成。并行组被配置为沿基线对齐其组件。第一个并行组包含第一个标签和第一个文本字段,第二个并行组包含第二个标签和第二个文本字段。
在这段代码中,要注意几个问题:
  • 不需要显式地将组件添加到容器;通过使用 Group 的一个 add 方法间接完成此操作。
  • 各种 add 方法返回调用者。这使得调用能很方便地进行链接。例如,group.addComponent(label1).addComponent(label2); 等效于group.addComponent(label1); group.addComponent(label2);
  • Group 没有公共构造方法;请使用 GroupLayout 的创建方法替代。
version1.2, 04/07/06since1.6