|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
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 物件:
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 屬性進行解釋的,而絕對值則不然。相關於基線的值是相對於基線進行計算的。有效值包括:
絕對值 |
相對於方向的值 |
相對於基線的值 |
|---|---|---|
GridBagConstraints.NORTHGridBagConstraints.SOUTHGridBagConstraints.WESTGridBagConstraints.EASTGridBagConstraints.NORTHWESTGridBagConstraints.NORTHEASTGridBagConstraints.SOUTHWESTGridBagConstraints.SOUTHEASTGridBagConstraints.CENTER(預設值) |
GridBagConstraints.PAGE_STARTGridBagConstraints.PAGE_ENDGridBagConstraints.LINE_STARTGridBagConstraints.LINE_ENDGridBagConstraints.FIRST_LINE_STARTGridBagConstraints.FIRST_LINE_ENDGridBagConstraints.LAST_LINE_STARTGridBagConstraints.LAST_LINE_END |
GridBagConstraints.BASELINEGridBagConstraints.BASELINE_LEADINGGridBagConstraints.BASELINE_TRAILINGGridBagConstraints.ABOVE_BASELINEGridBagConstraints.ABOVE_BASELINE_LEADINGGridBagConstraints.ABOVE_BASELINE_TRAILINGGridBagConstraints.BELOW_BASELINEGridBagConstraints.BELOW_BASELINE_LEADINGGridBagConstraints.BELOW_BASELINE_TRAILING |
GridBagConstraints.weightx、GridBagConstraints.weighty
weightx) 和列 (weighty) 中至少指定一個元件的權重,否則所有元件都會聚集在其容器的中央。這是因為,當權重為零(預設值)時,GridBagLayout 物件會將所有額外空間置於其單元網格和容器邊緣之間。
每行可以有一條基線,具體取決於該行中具有有效基線並沿此基線對齊的元件(元件的錨值是 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();
}
}
GridBagConstraints,
GridBagLayoutInfo,
ComponentOrientation,
序列化表格| 欄位摘要 | |
|---|---|
double[] |
columnWeights
此欄位保持對列權重的覆寫。 |
int[] |
columnWidths
此欄位保持對列最小寬度的覆寫。 |
protected Hashtable<Component,GridBagConstraints> |
comptable
此雜湊表維持元件與其網格包約束之間的關聯。 |
protected GridBagConstraints |
defaultConstraints
此欄位保持包含預設值的網格包約束實例,因此如果某個元件沒有與其相關聯的網格包約束,則會分派給該元件一個 defaultConstraints 的副本。 |
protected GridBagLayoutInfo |
layoutInfo
此欄位保持網格套件的佈局資訊。 |
protected static int |
MAXGRIDSIZE
此欄位不再用於保留陣列和保持向後相容性。 |
protected static int |
MINSIZE
網格包佈局可以佈置的最小網格。 |
protected static int |
PREFERREDSIZE
網格包佈局可以佈置的首選網格大小。 |
int[] |
rowHeights
此欄位保持對行最小高度的覆寫。 |
double[] |
rowWeights
此欄位保持對行權重的覆寫。 |
| 建構子摘要 | |
|---|---|
GridBagLayout()
創建網格包佈局管理器。 |
|
| 方法摘要 | |
|---|---|
void |
addLayoutComponent(Component comp,
Object constraints)
使用指定 constraints 物件將指定元件添加到佈局中。 |
void |
addLayoutComponent(String name,
Component comp)
無效,因為此佈局管理器不使用每元件字元串。 |
protected void |
adjustForGravity(GridBagConstraints constraints,
Rectangle r)
根據約束幾何結構和填充將 x、y、寬度和高度四個欄位調整為正確值。 |
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 中的資訊計算其所有者的最小大小。 |
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 容器的最小大小。 |
Dimension |
preferredLayoutSize(Container parent)
使用此網路包佈局確定 parent 容器的首選大小。 |
void |
removeLayoutComponent(Component comp)
從此佈局移除指定元件。 |
void |
setConstraints(Component comp,
GridBagConstraints constraints)
設置此佈局中指定元件的約束條件。 |
String |
toString()
返回此網格包佈局的值的字元串表示形式。 |
| 從類別 java.lang.Object 繼承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 欄位詳細資訊 |
|---|
protected static final int MAXGRIDSIZE
protected static final int MINSIZE
protected static final int PREFERREDSIZE
protected Hashtable<Component,GridBagConstraints> comptable
comptable 中的鍵是元件,值是 GridBagConstraints 的實例。
GridBagConstraintsprotected GridBagConstraints defaultConstraints
defaultConstraints 的副本。
getConstraints(Component),
setConstraints(Component, GridBagConstraints),
lookupConstraints(Component)protected GridBagLayoutInfo layoutInfo
layoutInfo 為 null,這指示網格包中不存在元件,或者即使存在,也是尚未經過驗證的元件。
getLayoutInfo(Container, int)public int[] columnWidths
null,則在計算全部最小列寬度之後將這些值應用到網格套件。如果 columnWidths 的元素數多於列數,則在網格包中添加列以比對 columnWidth 中的元素數。
getLayoutDimensions()public int[] rowHeights
null,則在計算全部最小行高度之後將這些值應用到網格套件。如果 rowHeights 的元素數多於行數,則在網格包中添加行以比對 rowHeights 中的元素數。
getLayoutDimensions()public double[] columnWeights
null,則在計算全部列權重之後將這些值應用到網格套件。如果 columnWeights[i] 大於列 i 的權重,則將 columnWeights[i] 中的權重分派給列 i。如果 columnWeights 的元素數多於列數,則多餘的元素將被忽略——而不會相應地創建更多列。
public double[] rowWeights
null,則在計算全部行權重之後將這些值應用到網格套件。如果 rowWeights[i] 大於行 i 的權重,則將 rowWeights[i] 中的權重分派給行 i。如果 rowWeights 的元素多於行數,則多餘的元素將被忽略——它們不會導致更多行的創建。
| 建構子詳細資訊 |
|---|
public GridBagLayout()
| 方法詳細資訊 |
|---|
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。值為 null 的 comp 值是無效的,返回 null。
comp - 要查詢的元件
public Point getLayoutOrigin()
ComponentOrientation 值如何。這與單元坐標給定的網格原點 (0,0) 不同。大多數應用程序並不直接調用此方法。
ComponentOrientationpublic int[][] getLayoutDimensions()
大多數應用程序並不直接調用此方法。
public double[][] getLayoutWeights()
大多數應用程序不直接調用此方法。
public Point location(int x,
int y)
(x, y) 指定的點。每個單元由其列索引(範圍從 0 到列數減 1)和其行索引(範圍從 0 到行數減 1)來標識。
如果 (x, y) 點位於網格的外部,則使用以下規則。如果 x 位於從左到右容器佈局的左邊或位於從右到左容器佈局的右邊,則列索引的返回值為 0。如果 x 位於從左到右容器佈局的右邊或位於從右到左容器佈局的左邊,則列索引的返回值是列數。如果 y 位於佈局的上邊,則行索引的返回值為零;如果 y 位於佈局的下邊,則行索引的返回值為行數。容器的方向由其 ComponentOrientation 屬性確定。
x - 點的 x 坐標y - 點的 y 坐標
ComponentOrientation
public void addLayoutComponent(String name,
Component comp)
LayoutManager 中的 addLayoutComponentname - 要與元件關聯的字元串comp - 要添加的元件
public void addLayoutComponent(Component comp,
Object constraints)
constraints 物件將指定元件添加到佈局中。注意,約束條件是可變的,因此快取記憶體時應該複製。
LayoutManager2 中的 addLayoutComponentcomp - 要添加的元件constraints - 確定如何將元件添加到佈局的物件
IllegalArgumentException - 如果 constraints 不是 GridBagConstraintpublic void removeLayoutComponent(Component comp)
大多數應用程序不直接調用此方法。
LayoutManager 中的 removeLayoutComponentcomp - 要移除的元件。Container.remove(java.awt.Component),
Container.removeAll()public Dimension preferredLayoutSize(Container parent)
parent 容器的首選大小。
大多數應用程序不直接調用此方法。
LayoutManager 中的 preferredLayoutSizeparent - 在其中進行佈局的容器
parent 容器的首選大小Container.getPreferredSize()public Dimension minimumLayoutSize(Container parent)
parent 容器的最小大小。
大多數應用程序不直接調用此方法。
LayoutManager 中的 minimumLayoutSizeparent - 在其中進行佈局的容器
parent 容器的最小大小Container.doLayout()public Dimension maximumLayoutSize(Container target)
LayoutManager2 中的 maximumLayoutSizetarget - 需要佈置的容器
Container,
minimumLayoutSize(Container),
preferredLayoutSize(Container)public float getLayoutAlignmentX(Container parent)
LayoutManager2 中的 getLayoutAlignmentX0.5fpublic float getLayoutAlignmentY(Container parent)
LayoutManager2 中的 getLayoutAlignmentY0.5fpublic void invalidateLayout(Container target)
LayoutManager2 中的 invalidateLayoutpublic void layoutContainer(Container parent)
GridBagLayout 物件的約束條件,此方法會重塑指定容器中的元件。
大多數應用程序不直接調用此方法。
LayoutManager 中的 layoutContainerparent - 要在其中進行佈局的容器Container,
Container.doLayout()public String toString()
Object 中的 toString
protected GridBagLayoutInfo getLayoutInfo(Container parent,
int sizeflag)
GridBagLayoutInfo 的實例。這需要通過子級集合傳遞三次:
此方法僅供 GridBagLayout 內部使用。
parent - 佈局容器sizeflag - PREFERREDSIZE 或 MINSIZE
GridBagLayoutInfo
protected GridBagLayoutInfo GetLayoutInfo(Container parent,
int sizeflag)
getLayoutInfo 來代替。此方法與 getLayoutInfo 相同;請參閱 getLayoutInfo 以獲取參數及返回值的詳細資訊。
protected void adjustForGravity(GridBagConstraints constraints,
Rectangle r)
GridBagLayout 內部使用。
constraints - 要應用的約束r - 要調整的 Rectangle
protected void AdjustForGravity(GridBagConstraints constraints,
Rectangle r)
adjustForGravity 來代替。此方法與 adjustForGravity 相同;請參閱 adjustForGravity 以獲取參數及返回值的詳細資訊。
protected Dimension getMinSize(Container parent,
GridBagLayoutInfo info)
getLayoutInfo 中的資訊計算其所有者的最小大小。此方法僅供 GridBagLayout 內部使用。
parent - 佈局容器info - 此父級的佈局資訊
Dimension 物件
protected Dimension GetMinSize(Container parent,
GridBagLayoutInfo info)
getMinSize 來代替。此方法與 getMinSize 相同;請參閱 getMinSize 以獲取參數及返回值的詳細資訊。
protected void arrangeGrid(Container parent)
GridBagLayout 內部使用。
parent - 佈局容器protected void ArrangeGrid(Container parent)
arrangeGrid 來代替。此方法與 arrangeGrid 相同;請參閱 arrangeGrid 以獲取參數及返回值的詳細資訊。
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。