所有类


java.awt
类 GridBagLayout

java.lang.Object
  继承者 java.awt.GridBagLayout
所有已实现的接口:
LayoutManager, LayoutManager2, Serializable

public class GridBagLayout
   
   
   
   
   
extends Object
implements LayoutManager2, Serializable

GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同即可将组件垂直和水平对齐。每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为显示区域

每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。除了 Constraints 对象之外,GridBagLayout 还考虑每个组件的最小和首选大小,以确定组件的大小。

网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。

为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。可以通过设置一个或多个实例变量来自定义 GridBagConstraints 对象:

GridBagConstraints.gridxGridBagConstraints.gridy
指定包含组件显示区域的前导角的单元,在此显示区域中,位于网格原点的单元地址是 gridx = 0gridy = 0。对于水平的从左到右的布局,组件的前导角是其左上角。对于水平的从右到左的布局,组件的前导角是其右上角。使用 GridBagConstraints.RELATIVE(默认值)指定将组件置于添加此组件前刚刚添加到容器组件的后面(沿 gridx 的 X 轴或 gridy 的 Y 轴)。
GridBagConstraints.gridwidthGridBagConstraints.gridheight
指定组件的显示区域中行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。使用 GridBagConstraints.REMAINDER 指定组件的显示区域为从 gridx 到该行(针对 gridwidth)中的最后一个单元,或者从 gridy 到该列(针对 gridheight)中的最后一个单元。 使用 GridBagConstraints.RELATIVE 指定组件的显示区域为从 gridx 到其所在行(针对 gridwidth)的倒数第二个单元,或者从 gridy 到其所在列(针对 gridheight)的倒数第二个单元。
GridBagConstraints.fill
当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。可能的值为 GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和 GridBagConstraints.BOTH(使组件完全填满其显示区域)。
GridBagConstraints.ipadxGridBagConstraints.ipady
指定布局中组件的内部填充,对组件最小大小的添加量。组件的宽度至少为其最小宽度加上 ipadx 像素。类似地,组件的高度至少为其最小高度加上 ipady 像素。
GridBagConstraints.insets
指定组件的外部填充,组件与其显示区域边缘之间间距的最小量。
GridBagConstraints.anchor
当组件小于其显示区域时,用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的 ComponentOrientation 属性,而绝对值则不然。有效值有:

绝对值

相对值

  • GridBagConstraints.NORTH
  • GridBagConstraints.SOUTH
  • GridBagConstraints.WEST
  • GridBagConstraints.EAST
  • GridBagConstraints.NORTHWEST
  • GridBagConstraints.NORTHEAST
  • GridBagConstraints.SOUTHWEST
  • GridBagConstraints.SOUTHEAST
  • GridBagConstraints.CENTER (the default)
  • 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.weightxGridBagConstraints.weighty
    用于确定分布空间的方式,这对于指定调整行为至关重要。除非在行 (weightx) 和列 (weighty) 中至少指定一个组件的权重,否则所有组件都会聚集在其容器的中央。这是因为,当权重为零(默认值)时,GridBagLayout 对象会将所有额外空间置于其单元网格和容器边缘之间。

    下图显示了由网格包布局管理的十个组件(均为按钮)。图 1 显示水平方向从左到右容器的布局,图 2 显示水平方向从右到左容器的布局。

    对此图形的描述见前文(图 1)。对此图形的描述见前文(图 2)。
    图 1:水平方向,从左到右图 2:水平方向,从右到左

    这十个组件的每一个都将与其相关的 GridBagConstraints 对象的 fill 字段设置为 GridBagConstraints.BOTH。此外,这些组件还有以下非默认值约束 (Constraints):

    • Button1、Button2、Button3: weightx = 1.0
    • Button4:weightx = 1.0gridwidth = GridBagConstraints.REMAINDER
    • Button5:gridwidth = GridBagConstraints.REMAINDER
    • Button6:gridwidth = GridBagConstraints.RELATIVE
    • Button7:gridwidth = GridBagConstraints.REMAINDER
    • Button8:gridheight = 2weighty = 1.0
    • Button9、Button 10: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();
         }
     }
     

    从以下版本开始:
    JDK1.0
    另请参见:
    GridBagConstraints, ComponentOrientation, 序列化表格

    字段摘要
     double[]columnWeights
              此字段保持对列权重的重写。
     int[]columnWidths
              此字段保持对列最小宽度的重写。
    protected  Hashtable<Component,GridBagConstraints>comptable
              此哈希表维持组件与其网格包约束之间的关联。
    protected  GridBagConstraintsdefaultConstraints
              此字段保持包含默认值的网格包约束实例,因此如果某个组件没有与其相关联的网格包约束,则会分配给该组件一个 defaultConstraints 的副本。
    protected  java.awt.GridBagLayoutInfolayoutInfo
              此字段保持网格包的布局信息。
    protected static intMAXGRIDSIZE
               
    protected static intMINSIZE
              网格包布局可以布置的最小网格。
    protected static intPREFERREDSIZE
              网格包布局可以布置的首选网格大小。
     int[]rowHeights
              此字段保持对行最小高度的重写。
     double[]rowWeights
              此字段保持对行权重的重写。
     
    构造方法摘要
    GridBagLayout()
              创建网格包布局管理器。
     
    方法摘要
     voidaddLayoutComponent(Component comp, Object constraints)
              使用指定 constraints 对象将指定组件添加到布局中。
     voidaddLayoutComponent(String name, Component comp)
              无效,因为此布局管理器不使用每组件字符串。
    protected  voidadjustForGravity(GridBagConstraints constraints, Rectangle r)
              根据约束几何结构和填充将 x、y、宽度和高度四个字段调整为正确值。
    protected  voidAdjustForGravity(GridBagConstraints constraints, Rectangle r)
              此方法已过时,仅提供向后兼容性;新代码应该调用 adjustForGravity 来代替。
    protected  voidarrangeGrid(Container parent)
              布置网格。
    protected  voidArrangeGrid(Container parent)
              此方法已过时,仅提供向后兼容性;新代码应该调用 arrangeGrid 来代替。
     GridBagConstraintsgetConstraints(Component comp)
              获得指定组件的约束。
     floatgetLayoutAlignmentX(Container parent)
              返回沿 X 轴的对齐方式。
     floatgetLayoutAlignmentY(Container parent)
              返回沿 y 轴的对齐方式。
     int[][]getLayoutDimensions()
              确定布局网格的列宽度和行高度。
    protected  java.awt.GridBagLayoutInfogetLayoutInfo(Container parent, int sizeflag)
              为当前托管子级的集合填充 GridBagLayoutInfo 的实例。
    protected  java.awt.GridBagLayoutInfoGetLayoutInfo(Container parent, int sizeflag)
              此方法已过时,仅提供向后兼容性;新代码应该调用 getLayoutInfo 来代替。
     PointgetLayoutOrigin()
              在目标容器的图形坐标空间确定布局区域的原点。
     double[][]getLayoutWeights()
              确定布局网格的行与列的权重。
    protected  DimensiongetMinSize(Container parent, java.awt.GridBagLayoutInfo info)
              基于 getLayoutInfo 中的信息计算其所有者的最小大小。
    protected  DimensionGetMinSize(Container parent, java.awt.GridBagLayoutInfo info)
              此方法已过时,仅提供向后兼容性;新代码应该调用 getMinSize 来代替。
     voidinvalidateLayout(Container target)
              使布局失效,指示如果布局管理器缓存了信息,则应该将其丢弃。
     voidlayoutContainer(Container parent)
              使用此网格包布局布置指定容器。
     Pointlocation(int x, int y)
              确定在布局网格中哪个单元包含由 (x, y) 指定的点。
    protected  GridBagConstraintslookupConstraints(Component comp)
              检索指定组件的约束。
     DimensionmaximumLayoutSize(Container target)
              在给出指定目标容器中的组件的前提下,返回此布局的最大维数
     DimensionminimumLayoutSize(Container parent)
              使用此网格包布局确定 parent 容器的最小大小。
     DimensionpreferredLayoutSize(Container parent)
              使用此网络包布局确定 parent 容器的首选大小。
     voidremoveLayoutComponent(Component comp)
              从此布局移除指定组件。
     voidsetConstraints(Component comp, GridBagConstraints constraints)
              设置此布局中指定组件的约束条件。
     StringtoString()
              返回此网格包布局的值的字符串表示形式。
     
    从类 java.lang.Object 继承的方法
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    字段详细信息

    MAXGRIDSIZE

    protected static final int MAXGRIDSIZE
    另请参见:
    常量字段值

    MINSIZE

    protected static final int MINSIZE
    网格包布局可以布置的最小网格。

    另请参见:
    常量字段值

    PREFERREDSIZE

    protected static final int PREFERREDSIZE
    网格包布局可以布置的首选网格大小。

    另请参见:
    常量字段值

    comptable

    protected Hashtable<Component,GridBagConstraints> comptable
    此哈希表维持组件与其网格包约束之间的关联。comptable 中的键是组件,值是 GridBagConstraints 的实例。

    另请参见:
    GridBagConstraints

    defaultConstraints

    protected GridBagConstraints defaultConstraints
    此字段保持包含默认值的网格包约束实例,因此如果某个组件没有与其相关联的网格包约束,则会分配给该组件一个 defaultConstraints 的副本。

    另请参见:
    getConstraints(Component), setConstraints(Component, GridBagConstraints), lookupConstraints(Component)