当前位置 类层次 (JDK) 所有类 (JDK) 所有域和方法 (JDK)

类 java.awt.GridBagLayout

java.lang.Object
   |
   +----java.awt.GridBagLayout

public class GridBagLayout
extends Object
implements LayoutManager2, Serializable

GridBagLayout 类是一个灵活的布局管理器,不需要组件的尺寸相同它就可以将组件在垂直方向上和水平方向上对齐。每个 GridBagLayout 对象保留一个动态的矩形单元网格,每个组件占用一个或多个单元,称为它的显示区域

每个由一个网格元包布局管理的组件都与一个 GridBagConstraints 的实例相关,它指定了组件在它的显示区域是如何放置的。

一个 GridBagLayout 对象放置一组组件的方式,取决于与每个组件相关的 GridBagConstraints 对象,并且取决于组件容器的最小尺寸和首选尺寸。

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

gridxgridy
指定组件显示区域左上方的单元,其中最左上角的单元地址为 gridx = 0gridy = 0 。使用 GridBagConstraints.RELATIVE (缺省值)来指定,该组件将被放置在,该组件被添加之前添加到容器中的组件的右边(为 gridx )或下边(为 gridy )。
gridwidthgridheight
在组件的显示区域指定在一行中单元的数目(为 gridwidth )或在一列中单元的数目(为 gridheight )。缺省值为 1 。使用 GridBagConstraints.REMAINDER 来指定该组件是在,    它所在行(为 gridwidth )或列(为 gridheight )的最后一个。使用 GridBagConstraints.RELATIVE 来指定该组件是在它所在行(为 gridwidth )或列(为     gridheight )的倒数第二个。
fill
当组件的显示区域大于组件所请求的尺寸时,用来确定是否(和如何)改变组件的大小。可能值为 GridBagConstraints.NONE (缺省), GridBagConstraints.HORIZONTAL (使组件的宽度在水平方向上足够大来填充它的显示区域,但是不改变它的高度), GridBagConstraints.VERTICAL (使组件的高度在垂直方向上足够大来填充它的显示区域,但是不改变它的宽度)和 GridBagConstraints.BOTH (使组件完全填充它的显示区域)。
ipadxipady
指定在布局内组件的内部补空,给组件的最小尺寸添加多少。组件的宽度至少为它的最小宽度加上 (ipadx * 2) 个像素(因为补空应用在组件的两边)。类似地,组件的高度至少为它的最小高度加上 (ipady * 2) 个像素。
insets
指定组件的外部补空,即在组件和它的显示区域边沿之间间距的最小量。
anchor
当组件小于它的显示区域时,用来确定在什么位置(显示区域内)放置该组件。有效值为 GridBagConstraints.CENTER (缺省), GridBagConstraints.NORTHGridBagConstraints.NORTHEASTGridBagConstraints.EASTGridBagConstraints.SOUTHEASTGridBagConstraints.SOUTHGridBagConstraints.SOUTHWESTGridBagConstraints.WESTGridBagConstraints.NORTHWEST
weightxweighty
用来确定如何分布空白区,对于指定改变大小的操作这是十分重要的。如果没有给至少一个组件指定一个权值,行中的权值为 (weightx) ,列中的权值为 (weighty) ,那么所有的组件会都堆在它们容器的中间。这是因为当权值为零时(缺省), GridBagLayout 对象将在它的单元网格和容器边沿之间任意放置额外的空白。

下列图形显示由网格包布局管理的十个组件(所有的按钮):

十个组件中的每一个都有它的与 GridBagConstraints 对象相关的 fill 域,并设置为 GridBagConstraints.BOTH 。另外,组件有下列非缺省约束:

下面是实现如上所示的示例的代码:


 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("Helvetica", 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

变量索引

columnWeights
columnWidths
comptable
defaultConstraints
layoutInfo
MAXGRIDSIZE
可以由网格元包布局来实现布局的网格位置的最大数值(水平的和垂直的)。
MINSIZE
可以由网格元包布局的实现布局的最小网格数。
PREFERREDSIZE
rowHeights
rowWeights

构造子索引

GridBagLayout()
创建一个网格元包布局管理器。

方法索引

addLayoutComponent(Component, Object)
利用指定的约束对象将指定的组件添加到布局。
addLayoutComponent(String, Component)
将带有指定名字的指定组件添加到布局中。
AdjustForGravity(GridBagConstraints, Rectangle)
ArrangeGrid(Container)
getConstraints(Component)
获取指定组件的约束。
getLayoutAlignmentX(Container)
返回沿 x 轴的对齐方式。
getLayoutAlignmentY(Container)
返回沿 y 轴的对齐方式。
getLayoutDimensions()
确定布局网格的列宽度和行高度。
GetLayoutInfo(Container, int)
打印出布局的约束。
getLayoutOrigin()
确定布局网格的原点。
getLayoutWeights()
确定布局网格列和行的权值。
GetMinSize(Container, GridBagLayoutInfo)
invalidateLayout(Container)
使布局无效,表示若布局管理器已存储了信息,则它应被删除。
layoutContainer(Container)
使用该网格元包布局来布局指定的容器。
location(int, int)
确定在布局网格的哪个单元包含由 (x , y) 指定的点。
lookupConstraints(Component)
检索指定组件的约束。
maximumLayoutSize(Container)
返回在指定的目标容器中指定组件对于该布局的最大尺寸。
minimumLayoutSize(Container)
确定使用该网格元包布局的目标容器的最小尺寸。
preferredLayoutSize(Container)
确定使用该网格元包布局的目标容器的首选尺寸。
removeLayoutComponent(Component)
从该布局中删除指定的组件。
setConstraints(Component, GridBagConstraints)
在该布局中设置指定组件的约束。
toString()
返回表示该网格元包布局值的字符串。

变量

MAXGRIDSIZE
 protected static final int MAXGRIDSIZE
可以由网格元包布局来实现布局的网格位置的最大数值(水平的和垂直的)。

MINSIZE
 protected static final int MINSIZE
可以由网格元包布局的实现布局的最小网格数。

PREFERREDSIZE
 protected static final int PREFERREDSIZE
comptable
 protected Hashtable comptable
defaultConstraints
 protected GridBagConstraints defaultConstraints
layoutInfo
 protected GridBagLayoutInfo layoutInfo
columnWidths
 public int columnWidths[]
rowHeights
 public int rowHeights[]
columnWeights
 public double columnWeights[]
rowWeights
 public double rowWeights[]

构造子

GridBagLayout
 public GridBagLayout()
创建一个网格元包布局管理器。


方法

setConstraints
 public void setConstraints(Component comp,
                            GridBagConstraints constraints)
在该布局中设置指定组件的约束。

参数:
comp - 要修改的组件。
constraints - 要被应用的约束。
getConstraints
 public GridBagConstraints getConstraints(Component comp)
获取指定组件的约束。 实际的 GridBagConstraints 对象的一个副本被返回。

参数:
comp - 将被查询的组件。
返回值:
在该网格元包布局中指定组件的约束;实际的约束对象的一个副本被返回。
lookupConstraints
 protected GridBagConstraints lookupConstraints(Component comp)
检索指定组件的约束。返回值不是一个副本,而是实际的由布局机制使用的 GridBagConstraints 对象。

参数:
comp - 要被查询的组件
返回值:
指定组件的约束。
getLayoutOrigin
 public Point getLayoutOrigin()
确定布局网格的原点。大多数应用程序不直接调用该方法。

返回值:
在布局网格左上角的原点单元。
getLayoutDimensions
 public int[][] getLayoutDimensions()
确定布局网格的列宽度和行高度。

大多数应用程序不直接调用该方法。

返回值:
一个二维数组,包含布局列的宽度和布局行的高度。
getLayoutWeights
 public double[][] getLayoutWeights()
确定布局网格列和行的权值。如果布局有额外的空间来填充,那么权值被用于计算指定的列或行伸展超过首选尺寸多少。

大多数应用程序不直接调用该方法。

返回值:
一个二维数组,表示布局列的水平权值和布局行的垂直权值。
location
 public Point location(int x,
                                             			  int y)
确定在布局网格的哪个单元包含由 (x , y) 指定的点。每个单元由它的列索引(范围从 0 到列数减 1 )和它的行索引(范围从 0 到行数减 1 )来标识。

如果 (x, y) 点位于网格的外部,那么下列规则将被使用。如果 x 位于布局的左边列索引返回零,如果 x 位于布局的右边列索引返回列数。如果 y 位于布局的上边列索引返回零,如果 y 位于布局的下边列索引返回行数。

参数:
x - 点的 x 坐标。
y - 点的 y 坐标。
返回值:
索引的一个有序对,指示在布局网格的哪个单元中包含点 (x ,  y) 。
addLayoutComponent
 public void addLayoutComponent(String name,
                                Component comp)
将带有指定名字的指定组件添加到布局中。

参数:
name - 组件的名字。
comp - 要被添加的组件。
addLayoutComponent
 public void addLayoutComponent(Component comp,
                                Object constraints)
使用指定的约束对象将指定的组件添加到布局。

参数:
comp - 要被添加的组件。
constraints - 确定组件被添加到布局中的方式的对象。
removeLayoutComponent
 public void removeLayoutComponent(Component comp)
从该布局中删除指定的组件。

大多数应用程序不直接调用该方法。

参数:
comp - 要被删除的组件。
参见:
remove, removeAll
preferredLayoutSize
 public Dimension preferredLayoutSize(Container parent)
确定使用该网格元包布局的目标容器的首选尺寸。

大多数应用程序不直接调用该方法。

参数:
target - 要在其中进行布局的容器。
参见:
getPreferredSize
minimumLayoutSize
 public Dimension minimumLayoutSize(Container parent)
确定使用该网格元包布局的目标容器的最小尺寸。

大多数应用程序不直接调用该方法。

参数:
target - 要在其中进行布局的容器。
参见:
doLayout
maximumLayoutSize
 public Dimension maximumLayoutSize(Container target)
返回在指定的目标容器中指定组件对于该布局的最大尺寸。

参数:
target - 要被布局的组件
参见:
Container, minimumLayoutSize, preferredLayoutSize
getLayoutAlignmentX
 public float getLayoutAlignmentX(Container parent)
返回沿 x 轴的对齐方式。它指定了这个组件相对于其它组件的对齐方式。该值应是一个介于 0 和 1 之间的数,其中 0 表示沿原点对齐,1 表示按距原点最远的点对齐,0.5 表示居中对齐等。

getLayoutAlignmentY
 public float getLayoutAlignmentY(Container parent)
返回沿 y 轴的对齐方式。 它指定了这个组件相对于其它组件的对齐方式。该值应是一个介于 0 和 1 之间的数,其中 0 表示沿原点对齐,1 表示按距原点最远的点对齐,0.5 表示居中对齐等。

invalidateLayout
 public void invalidateLayout(Container target)
使布局无效,表示若布局管理器已存储了信息,则它应被删除。

layoutContainer
 public void layoutContainer(Container parent)
使用该网格元包布局来布局指定的容器。该方法在指定的容器中调整组件,以满足该 GridBagLayout 对象的约束。

大多数应用程序不直接调用该方法。

参数:
parent - 要在其中进行布局的容器。
参见:
Container, doLayout
toString
 public String toString()
返回表示该网格元包布局值的字符串。

返回值:
表示该网格元包布局的字符串。
覆盖:
Object 中的 toString
GetLayoutInfo
 protected GridBagLayoutInfo GetLayoutInfo(Container parent,
                                           int sizeflag)
打印出布局的约束。对于调试有用。

AdjustForGravity
 protected void AdjustForGravity(GridBagConstraints constraints,
                                 Rectangle r)
GetMinSize
 protected Dimension GetMinSize(Container parent,
                                GridBagLayoutInfo info)
ArrangeGrid
 protected void ArrangeGrid(Container parent)

当前位置 类层次 (JDK) 所有类 (JDK) 所有域和方法 (JDK)