public class GridBagLayout extends Object implements LayoutManager2, Serializable
GridBagLayout类是一种灵活的布局管理器,可以垂直,水平或沿其基线对齐组件,而不需要组件的大小相同。
每个GridBagLayout对象维护一个动态的,矩形的单元格网格,每个组件占据一个或多个单元格,称为其显示区域 。
通过管理每个组件GridBagLayout与实例相关联的GridBagConstraints 。 constraints对象指定组件的显示区域在网格上的位置以及组件在其显示区域内的位置。 除了其约束对象外, GridBagLayout还考虑每个组件的最小和首选大小,以确定组件的大小。
电网的整体方向取决于容器的ComponentOrientation属性。 对于水平从左到右的方向,网格坐标(0,0)位于容器的左上角,其中x向右增加,y向下增加。 对于水平从右到左的方向,网格坐标(0,0)位于容器的右上角,其中x向左增加,y向下增加。
要有效地使用网格包布局,您必须自定义与其组件相关联的一个或多个GridBagConstraints对象。 您可以通过设置一个或多个实例变量来自定义一个GridBagConstraints对象:
GridBagConstraints.gridx , GridBagConstraints.gridy
gridx = 0 , gridy = 0 。
对于水平从左到右的布局,组件的前角是其左上角。
对于水平从右到左的布局,组件的前角是其右上角。
使用GridBagConstraints.RELATIVE (默认值)来指定在添加该组件之前添加到容器的组件,紧接着(沿着x轴为gridx或y轴为gridy )放置组件。
GridBagConstraints.gridwidth , GridBagConstraints.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.ipadx , GridBagConstraints.ipady
ipadx像素。
类似地,组件的高度将至少为最小高度加上ipady像素。
GridBagConstraints.insets
GridBagConstraints.anchor
ComponentOrientation属性进行解释,绝对值不是。
基线相对值相对于基线计算。
有效值为:
Absolute Values
Orientation Relative Values
Baseline Relative Values
GridBagConstraints.NORTHGridBagConstraints.SOUTHGridBagConstraints.WESTGridBagConstraints.EASTGridBagConstraints.NORTHWESTGridBagConstraints.NORTHEASTGridBagConstraints.SOUTHWESTGridBagConstraints.SOUTHEASTGridBagConstraints.CENTER (the default)GridBagConstraints.PAGE_STARTGridBagConstraints.PAGE_ENDGridBagConstraints.LINE_STARTGridBagConstraints.LINE_ENDGridBagConstraints.FIRST_LINE_STARTGridBagConstraints.FIRST_LINE_ENDGridBagConstraints.LAST_LINE_STARTGridBagConstraints.LAST_LINE_ENDGridBagConstraints.BASELINEGridBagConstraints.BASELINE_LEADINGGridBagConstraints.BASELINE_TRAILINGGridBagConstraints.ABOVE_BASELINEGridBagConstraints.ABOVE_BASELINE_LEADINGGridBagConstraints.ABOVE_BASELINE_TRAILINGGridBagConstraints.BELOW_BASELINEGridBagConstraints.BELOW_BASELINE_LEADINGGridBagConstraints.BELOW_BASELINE_TRAILINGGridBagConstraints.weightx , GridBagConstraints.weighty
weightx )和列( weighty )的权重,否则所有组件都将集中在其容器的中心。
这是因为当重量为零(默认值)时, GridBagLayout对象在其单元格格栅和容器边缘之间放置任何额外的空格。
每行可能有一个基线; 基线是由该行中具有有效基线和沿基线对齐(该组件的锚定值中的一个的分量确定BASELINE , BASELINE_LEADING或BASELINE_TRAILING )。 如果行中没有一个组件具有有效的基线,则该行不具有基线。
如果组件跨越行,则它将与起始行的基线对齐(如果baseline-resize行为为CONSTANT_ASCENT )或结束行(如果baseline-resize行为为CONSTANT_DESCENT )。 组件对齐的行称为主要行 。
下图显示了基线布局,并包括跨越行的组件:
CONSTANT_DESCENT和具有锚BASELINE 。 由于基准调整大小的行为为CONSTANT_DESCENT ,面板的主要行为第1行。 CENTER_OFFSET ,锚点为BASELINE 。 使用一个基线相对值定位的组件与使用绝对值或方向相对值定位时的大小不同。 组件变化的方式由当前行的基线如何变化决定。 如果具有相同主行的任何组件的基线调整大小行为为CONSTANT_DESCENT ,则基线将锚定到显示区域的CONSTANT_DESCENT ,否则基线将锚定到显示区域的顶部。 以下规则规定了调整大小的行为:
OTHER ,则位于基准线上的可调整大小的OTHER仅在调整大小的基准符合显示区域时才调整大小。 如果基准使得它不适合显示区域,则组件不会调整大小。 OTHER只能高达display height - baseline + baseline of component 。 以下图显示了由网格包布局管理的十个组件(所有按钮)。 图2显示了一个水平的从左到右的容器的布局,图3显示了一个水平的,从右到左的容器的布局。
Figure 2: Horizontal, Left-to-Right Figure 3: Horizontal, Right-to-Left
十个组件中的每一个都具有其关联的GridBagConstraints对象的fill字段设置为GridBagConstraints.BOTH 。 此外,组件具有以下非默认约束:
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();
}
}
GridBagConstraints , GridBagLayoutInfo , ComponentOrientation , Serialized Form
| Modifier and Type | Field and Description |
|---|---|
double[] |
columnWeights
此字段保留对列权重的覆盖。
|
int[] |
columnWidths
此字段保留对列最小宽度的覆盖。
|
protected Hashtable<Component,GridBagConstraints> |
comptable
这个散列表维护了一个组件与其gridbag约束之间的关联。
|
protected GridBagConstraints |
defaultConstraints
此字段保持包含默认值的网格包约束实例,因此,如果一个组件不具有与其相关联的网格包约束,则该组件将被分配的副本
defaultConstraints 。
|
protected GridBagLayoutInfo |
layoutInfo
该字段保存网格包的布局信息。
|
protected static int |
MAXGRIDSIZE
该字段不再用于保留阵列并保留向后兼容性。
|
protected static int |
MINSIZE
可以通过网格布局布局的最小网格。
|
protected static int |
PREFERREDSIZE
可以通过网格布局布局的首选网格尺寸。
|
int[] |
rowHeights
此字段将覆盖行的最小高度。
|
double[] |
rowWeights
该字段保留对行权重的覆盖。
|
| Constructor and Description |
|---|
GridBagLayout()
创建网格包布局管理器。
|
| Modifier and Type | Method and Description |
|---|---|
void |
addLayoutComponent(Component comp, Object constraints)
使用指定的
constraints对象将指定的组件添加到布局。
|
void |
addLayoutComponent(String name, Component comp)
没有效果,因为此布局管理器不使用每个组件字符串。
|
protected void |
adjustForGravity(GridBagConstraints constraints, Rectangle r)
根据约束几何和焊盘,将x,y,width和height字段调整为正确的值。
|
protected void |
AdjustForGravity(GridBagConstraints constraints, Rectangle r)
此方法已过时,仅供后向兼容;
新代码应该调用 adjustForGravity 。
|
protected void |
arrangeGrid(Container parent)
排出电网。
|
protected void |
ArrangeGrid(Container parent)
此方法已过时,仅供后向兼容;
新代码应该调用 arrangeGrid 。
|
GridBagConstraints |
getConstraints(Component comp)
获取指定组件的约束。
|
float |
getLayoutAlignmentX(Container parent)
返回沿x轴的对齐方式。
|
float |
getLayoutAlignmentY(Container parent)
返回沿着y轴的对齐。
|
int[][] |
getLayoutDimensions()
确定布局网格的列宽和行高。
|
protected GridBagLayoutInfo |
getLayoutInfo(Container parent, int sizeflag)
填写当前管理的孩子组的
GridBagLayoutInfo的一个实例。
|
protected GridBagLayoutInfo |
GetLayoutInfo(Container parent, int sizeflag)
此方法已过时,仅供后向兼容;
新代码应该调用 getLayoutInfo 。
|
Point |
getLayoutOrigin()
确定目标容器的图形坐标空间中的布局区域的原点。
|
double[][] |
getLayoutWeights()
确定布局网格的列和行的权重。
|
protected Dimension |
getMinSize(Container parent, GridBagLayoutInfo info)
根据getLayoutInfo的信息,
getLayoutInfo主站的最小尺寸。
|
protected Dimension |
GetMinSize(Container parent, GridBagLayoutInfo info)
此方法已过时,仅供后向兼容;
新代码应该调用 getMinSize 。
|
void |
invalidateLayout(Container target)
使布局无效,指示如果布局管理器已缓存信息,则应将其丢弃。
|
void |
layoutContainer(Container parent)
使用此网格包布局来放出指定的容器。
|
Point |
location(int x, int y)
确定哪个小区在布局网格包含由指定的点
(x, y) 。
|
protected GridBagConstraints |
lookupConstraints(Component comp)
检索指定组件的约束。
|
Dimension |
maximumLayoutSize(Container target)
给定指定目标容器中的组件的返回布局的最大尺寸。
|
Dimension |
minimumLayoutSize(Container parent)
使用此网格包布局
parent parent容器的最小尺寸。
|
Dimension |
preferredLayoutSize(Container parent)
使用此网格包布局
parent parent容器的首选大小。
|
void |
removeLayoutComponent(Component comp)
从此布局中删除指定的组件。
|
void |
setConstraints(Component comp, GridBagConstraints constraints)
在此布局中设置指定组件的约束。
|
String |
toString()
返回此网格包布局值的字符串表示形式。
|
protected static final int MAXGRIDSIZE
protected static final int MINSIZE
protected static final int PREFERREDSIZE
protected Hashtable<Component,GridBagConstraints> comptable
comptable中的键是组件,值是GridBagConstraints的GridBagConstraints 。
GridBagConstraints
protected GridBagConstraints defaultConstraints
defaultConstraints的副本。
protected GridBagLayoutInfo layoutInfo
layoutInfo是null这表示在gridbag中没有组件,或者如果有组件,它们尚未被验证。
public int[] columnWidths
null ,则在计算所有最小列宽度后,将值应用于网格包。
如果columnWidths具有比列数更多的元素,列将添加到gridbag中以匹配columnWidth中的元素数。
getLayoutDimensions()
public int[] rowHeights
null ,则在计算所有最小行高后,将值应用于网格包。
如果rowHeights具有比行数更多的元素,则将行添加到rowHeights以匹配rowHeights中的元素rowHeights 。
getLayoutDimensions()
public double[] columnWeights
null ,则在计算所有列权重之后,将值应用于网格包。
如果columnWeights[i] >重量为列i,那么列i的重量在columnWeights[i] 。
如果columnWeights具有比列数更多的元素,则会忽略多余的元素 - 它们不会导致创建更多的列。
public double[] rowWeights
null ,则在计算所有行权重之后,将值应用于网格包。
如果rowWeights[i] >重量为i行,那么行i被分配在rowWeights[i]的权重。
如果rowWeights具有比行数更多的元素,则会忽略多余的元素 - 它们不会导致创建更多的行。
public void setConstraints(Component comp, GridBagConstraints constraints)
comp - 要修改的组件
constraints - 要应用的约束
public GridBagConstraints getConstraints(Component comp)
GridBagConstraints对象的副本。
comp - 要查询的组件
protected GridBagConstraints lookupConstraints(Component comp)
GridBagConstraints对象。
如果comp不在GridBagLayout ,则返回一组默认值GridBagConstraints 。 comp价值null无效并返回null 。
comp - 要查询的组件
public Point getLayoutOrigin()
ComponentOrientation值。
这与由单元格坐标(0,0)给出的网格原点不同。
大多数应用程序不直接调用此方法。
ComponentOrientation
public int[][] getLayoutDimensions()
大多数应用程序不直接调用此方法。
public double[][] getLayoutWeights()
大多数应用程序不直接调用此方法。
public Point location(int x, int y)
(x, y) 。
每个单元格通过其列索引(范围从0到列数减1)和其行索引(范围从0到行数减1)来标识。
如果(x, y)点位于网格外,则使用以下规则。 如果x位于左至右x布局左侧或右至左容器布局右侧,列索引返回为零。 如果x位于x中的布局右侧或右至左容器中的左侧,则列索引将返回为列数。 如果y位于布局之上,行索引将返回为零,以及y位于布局下方的行数。 容器的方向由其ComponentOrientation属性确定。
x - 一个点的
x坐标
y - 点的
y坐标
ComponentOrientation
public void addLayoutComponent(String name, Component comp)
addLayoutComponent在界面
LayoutManager
name - 与
name的字符串
comp - 要添加的组件
public void addLayoutComponent(Component comp, Object constraints)
constraints对象将指定的组件添加到布局。
请注意,约束是可变的,因此在缓存时被克隆。
addLayoutComponent在界面
LayoutManager2
comp - 要添加的组件
constraints - 确定组件如何添加到布局的对象
IllegalArgumentException - 如果
constraints不是
GridBagConstraint
public void removeLayoutComponent(Component comp)
大多数应用程序不直接调用此方法。
removeLayoutComponent在界面
LayoutManager
comp - 要移除的组件。
Container.remove(java.awt.Component) ,
Container.removeAll()
public Dimension preferredLayoutSize(Container parent)
parent parent容器的首选大小。
大多数应用程序不直接调用此方法。
preferredLayoutSize在界面
LayoutManager
parent - 进行布局的容器
parent容器
Container.getPreferredSize()
public Dimension minimumLayoutSize(Container parent)
parent parent容器的最小尺寸。
大多数应用程序不直接调用此方法。
minimumLayoutSize在界面
LayoutManager
parent - 要做布局的容器
parent容器的最小尺寸
Container.doLayout()
public Dimension maximumLayoutSize(Container target)
maximumLayoutSize在界面
LayoutManager2
target - 需要布置的容器
Container , minimumLayoutSize(Container) , preferredLayoutSize(Container)
public float getLayoutAlignmentX(Container parent)
getLayoutAlignmentX在界面
LayoutManager2
0.5f指示居中
public float getLayoutAlignmentY(Container parent)
getLayoutAlignmentY在界面
LayoutManager2
0.5f表示居中
public void invalidateLayout(Container target)
invalidateLayout在界面
LayoutManager2
public void layoutContainer(Container parent)
GridBagLayout对象的GridBagLayout 。
大多数应用程序不直接调用此方法。
layoutContainer在界面
LayoutManager
parent - 进行布局的容器
Container , Container.doLayout()
protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag)
GridBagLayoutInfo的一个实例。
这需要通过一组孩子三次:
此方法只能由GridBagLayout使用GridBagLayout 。
parent - 布局容器
sizeflag -
PREFERREDSIZE或
MINSIZE
GridBagLayoutInfo为一套儿童
protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag)
protected void adjustForGravity(GridBagConstraints constraints, Rectangle r)
GridBagLayout使用GridBagLayout 。
constraints - 要应用的约束
r - 要调整的
Rectangle
protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r)
adjustForGravity 。
此方法与adjustForGravity相同;
有关adjustForGravity的详细信息,请参阅adjustForGravity。
protected Dimension getMinSize(Container parent, GridBagLayoutInfo info)
getLayoutInfo出主机的最小尺寸。
此方法只能由GridBagLayout使用GridBagLayout 。
parent - 布局容器
info - 这个父母的布局信息
Dimension最小大小的
Dimension对象
protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info)
protected void arrangeGrid(Container parent)
GridBagLayout 。
parent - 布局容器
protected void ArrangeGrid(Container parent)
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.