分类: Java
2009-03-15 15:30:13
java.lang.Objectjava.awt.GridBagLayout
public class GridBagLayout
GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同便可以将组件垂直、水平或沿它们的基线对齐。每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,该单元被称为显示区域。
每个由 GridBagLayout 管理的组件都与GridBagConstraints的实例相关联。Constraints 对象指定组件的显示区域在网格中的具体放置位置,以及组件在其显示区域中的放置方式。除了 Constraints 对象之外,GridBagLayout 还考虑每个组件的最小大小和首选大小,以确定组件的大小。
网格的总体方向取决于容器的 ComponentOrientation属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。
为了有效使用网格包布局,必须自定义与组件关联的一个或多个 GridBagConstraints 对象。可以通过设置一个或多个实例变量来自定义 GridBagConstraints 对象:
gridx = 0, gridy = 0。对于水平的从左到右的布局,组件的前导角是其左上角。对于水平的从右到左的布局,组件的前导角是其右上角。使用 GridBagConstraints.RELATIVE(默认值),指定会将组件直接放置在之前刚添加到容器中的组件的后面(沿 X 轴向为 gridx 或 Y 轴向为 gridy)。
gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。使用 GridBagConstraints.REMAINDER 指定组件的显示区域,该区域的范围是从 gridx 到该行(针对 gridwidth)中的最后一个单元,或者从 gridy 到该列(针对 gridheight)中的最后一个单元。 使用 GridBagConstraints.RELATIVE 指定组件的显示区域,该区域的范围是从 gridx 到其所在行(针对 gridwidth)的倒数第二个单元,或者从 gridy 到其所在列(针对 gridheight)的倒数第二个单元。
GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和 GridBagConstraints.BOTH(使组件完全填满其显示区域)。
ipadx 像素。类似地,组件的高度至少为其最小高度加上 ipady 像素。
ComponentOrientation 属性进行解释的,而绝对值则不然。相关于基线的值是相对于基线进行计算的。有效值包括:
|
绝对值 |
相对于方向的值 |
相对于基线的值 |
|---|---|---|
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER(默认值) |
GridBagConstraints.PAGE_START
GridBagConstraints.PAGE_END
GridBagConstraints.LINE_START
GridBagConstraints.LINE_END
GridBagConstraints.FIRST_LINE_START
GridBagConstraints.FIRST_LINE_END
GridBagConstraints.LAST_LINE_START
GridBagConstraints.LAST_LINE_END |
GridBagConstraints.BASELINE
GridBagConstraints.BASELINE_LEADING
GridBagConstraints.BASELINE_TRAILING
GridBagConstraints.ABOVE_BASELINE
GridBagConstraints.ABOVE_BASELINE_LEADING
GridBagConstraints.ABOVE_BASELINE_TRAILING
GridBagConstraints.BELOW_BASELINE
GridBagConstraints.BELOW_BASELINE_LEADING
GridBagConstraints.BELOW_BASELINE_TRAILING |
weightx) 和列 (weighty) 中至少指定一个组件的权重,否则所有组件都会聚集在其容器的中央。这是因为,当权重为零(默认值)时,GridBagLayout 对象会将所有额外空间置于其单元网格和容器边缘之间。添加的按钮时,在容器中一般只是占一小部分空间,没有被按钮占有的区域称为额外区域,该额外区域会随着weightx和weighty值而分配。每行可以有一条基线,具体取决于该行中具有有效基线并沿此基线对齐的组件(组件的锚值是 BASELINE、BASELINE_LEADING 或 BASELINE_TRAILING 其中之一)。如果行中没有具有有效基线的组件,则该行没有基线。
如果组件跨多行,则它与起始行(如果基线调整行为是 CONSTANT_ASCENT)或结束行(如果基线调整行为是 CONSTANT_DESCENT)的基线对齐。用来对齐组件的行称为主导行。
下图显示了基线布局并包括横跨行的组件:
|
CONSTANT_DESCENT 基线调整行为以及 BASELINE 的锚。因为基线调整行为是 CONSTANT_DESCENT,所以该面板的主要行是第 1 行。
CENTER_OFFSET 基线调整行为和 BASELINE 的锚。 使用一个相对于基线的值定位的组件调整不同于使用绝对值或相对于方向的值。组件更改的方式由主要行的基线更改方式指示。如果基于相同主导行的所有组件具有 CONSTANT_DESCENT 基线调整行为,则基线定位到显示区域底部;否则,基线定位到显示区域顶部。下述规则指示调整大小的行为:
OTHER 基线调整行为的可调整大小组件。如果不能将基线放入显示区域,则该组件不能调整大小。
OTHER 基线调整行为的组件只能增长到和显示高度 - 基线 + 组件基线一样高。 下图显示了由网格包布局管理的十个组件(均为按钮)。图 2 显示水平方向从左到右的容器的布局,图 3 显示水平方向从右到左的容器的布局。
|
|
| 图 2:水平方向,从左到右 | 图 3:水平方向,从右到左 |
十个组件的每一个组件都会将与之相关的 GridBagConstraints 对象的 fill 字段设置为 GridBagConstraints.BOTH。此外,这些组件还具有以下非默认值约束 (Constraints):
weightx = 1.0
weightx = 1.0、gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.RELATIVE
gridwidth = GridBagConstraints.REMAINDER
gridheight = 2、weighty = 1.0
gridwidth = GridBagConstraints.REMAINDER 下面是实现上述示例的代码:
import java.awt.*; import java.util.*; import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("SansSerif", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c); setSize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1); f.pack(); f.setSize(f.getPreferredSize()); f.show(); } }(摘自JDK API 1.6.0)