|
JavaTM 2 Platform Std. Ed. v1.4.0 |
||||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | ||||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | ||||||||||
java.lang.Object | +--java.awt.GridBagLayout
GridBagLayout クラスは、異なる大きさのコンポーネントでも縦横に配置できる柔軟なレイアウトマネージャです。それぞれの GridBagLayout オブジェクトは、セルによって構成される動的な矩形グリッドを格納しています。各コンポーネントは、1 つ以上のセル (「表示領域」と呼ぶ) に配置されます。
GridBagLayout によって管理される各コンポーネントは、GridBagConstraints のインスタンスと関連します。制約オブジェクトは、コンポーネントの表示領域をグリッド上に配置する場所、およびコンポーネントをその表示領域内に配置する方法を指定します。また、この制約オブジェクトに加え、GridBagLayout は、コンポーネントのサイズを決定するために、各コンポーネントの最小サイズおよび推奨サイズを考慮します。
グリッドの全体の方向は、コンテナの ComponentOrientation プロパティによって決まります。水平方向に左から右の場合、グリッドの座標 (0,0) はコンテナの左上隅になります。この場合、X 座標は右方向に、Y 座標は下方向に値が増加します。水平方向に右から左の場合、グリッドの座標 (0,0) はコンテナの右上隅になります。この場合、X 座標は左方向に、Y 座標は下方向に値が増加します。
グリッドバッグレイアウトを効果的に利用するには、コンポーネントの少なくとも 1 つの GridBagConstraints オブジェクトをカスタマイズする必要があります。GridBagConstraints オブジェクトをカスタマイズするには、1 つまたは複数のインスタンス変数を設定します。
GridBagConstraints.gridx、GridBagConstraints.gridy
gridx = 0、gridy = 0 です。水平方向に左から右へのレイアウトの場合、コンポーネントのリーディングコーナーは左上隅です。水平方向に右から左へのレイアウトの場合、コンポーネントのリーディングコーナーは右上隅です。GridBagConstraints.RELATIVE (規定値) を使用して、コンポーネントをこのコンポーネントが追加される直前にコンテナに追加されたコンポーネントの直後に (gridx は x 軸に沿って、gridy は y 軸に沿って) 配置するよう指定します。
GridBagConstraints.gridwidth, GridBagConstraints.gridheight
gridwidth) または 1 列 (gridheight) あたりのセルの数を指定します。既定値は 1 です。コンポーネントが行 (gridwidth) または列 (gridheight) の最後であることを指定する場合は、GridBagConstraints.REMAINDER を設定します。次のコンポーネントで、行 (gridwidth) または列 (gridheight) が終了することを指定する場合は、GridBagConstraints.RELATIVE を設定します。
GridBagConstraints.fill
GridBagConstraints.NONE (デフォルト)、GridBagConstraints.HORIZONTAL (コンポーネントの高さは変更せずに、幅を表示領域いっぱいにする)、GridBagConstraints.VERTICAL (コンポーネントの幅は変更せずに、高さを表示領域いっぱいにする)、GridBagConstraints.BOTH (コンポーネントを表示領域いっぱいにする) です。
GridBagConstraints.ipadx、GridBagConstraints.ipady
(ipadx * 2) ピクセルとなります (コンポーネントの両側にパディングされるため)。同様に、コンポーネントの高さは、少なくとも最小の高さ + (ipady * 2) ピクセルとなります。
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.weightx, GridBagConstraints.weighty
weightx) または 1 列 (weighty) につき、少なくとも 1 つのコンポーネントにウェイトを設定しないかぎり、すべてのコンポーネントがコンテナの中央に集まります。これは、ウェイトが 0 の場合 (デフォルト) には、GridBagLayout オブジェクトが余分なスペースをすべてセルのグリッドとコンテナの端の間に配置してしまうからです。
次に示す図は、グリッドバッグレイアウトによって管理される 10 個のコンポーネント (すべてのボタン) を示します。図 1 は水平方向に左から右へのコンテナを示し、図 2 は水平方向に右から左へのコンテナを示します。
![]() |
![]() |
| 図 1: 水平方向に左から右 | 図 2: 水平方向に右から左 |
10 個のコンポーネントのそれぞれは、その関連している 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("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();
}
}
GridBagConstraints,
ComponentOrientation,
直列化された形式| フィールドの概要 | |
double[] |
columnWeights
このフィールドは、列のウェイトに対するオーバーライドを保持します。 |
int[] |
columnWidths
このフィールドは、列の最小幅に対するオーバーライドを保持します。 |
protected Hashtable |
comptable
このハッシュテーブルは、コンポーネントとコンポーネントのグリッドバッグ制約との関連性を維持します。 |
protected GridBagConstraints |
defaultConstraints
このフィールドは、既定値を格納するグリッドバッグ制約のインスタンスを保持します。 |
protected java.awt.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)
このメソッドは下位互換性のためだけに提供されます。 |
protected void |
arrangeGrid(Container parent)
グリッドを配置します。 |
protected void |
ArrangeGrid(Container parent)
このメソッドは下位互換性のためだけに提供されます。 |
GridBagConstraints |
getConstraints(Component comp)
指定されたコンポーネントの制約を返します。 |
float |
getLayoutAlignmentX(Container parent)
x 軸方向の配置方法を返します。 |
float |
getLayoutAlignmentY(Container parent)
y 軸方向の配置方法を返します。 |
int[][] |
getLayoutDimensions()
レイアウトグリッドの列の幅と行の高さを指定します。 |
protected java.awt.GridBagLayoutInfo |
getLayoutInfo(Container parent,
int sizeflag)
管理される子の現在のセットに対する上記構造のインスタンスに値を入れます。 |
protected java.awt.GridBagLayoutInfo |
GetLayoutInfo(Container parent,
int sizeflag)
このメソッドは下位互換性のためだけに提供されます。 |
Point |
getLayoutOrigin()
ターゲットコンテナのグラフィックス座標空間内で、レイアウト領域の原点を指定します。 |
double[][] |
getLayoutWeights()
レイアウトグリッドの列と行のウェイトを指定します。 |
protected Dimension |
getMinSize(Container parent,
java.awt.GridBagLayoutInfo info)
getLayoutInfo() の情報に基づきマスターの最小サイズを計算します。 |
protected Dimension |
GetMinSize(Container parent,
java.awt.GridBagLayoutInfo info)
このメソッドは下位互換性のためだけに提供されます。 |
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)
このグリッドバッグレイアウトを使って、 target コンテナの最小サイズを指定します。
|
Dimension |
preferredLayoutSize(Container parent)
このグリッドバッグレイアウトを使って、 target コンテナの推奨サイズを指定します。
|
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 comptable
GridBagConstraints のインスタンスです。
GridBagConstraintsprotected GridBagConstraints defaultConstraints
defaultConstraints のコピーが割り当てられます。
getConstraints(Component),
setConstraints(Component, GridBagConstraints),
lookupConstraints(Component)protected java.awt.GridBagLayoutInfo layoutInfo
layoutInfo が null の場合は、グリッドバッグにコンポーネントがないか、またはコンポーネントがあってもそれがまだ検査を受けていないことを意味します。
getLayoutInfo(Container, int)public int[] columnWidths
null ではない場合、すべての最小列幅が計算されたあとで、その値がグリッドバッグに適用されます。columnWidths に列数より多くの要素がある場合、columnWidth の要素数に合わせるためにグリッドバッグに列が追加されます。
getLayoutDimensions()public int[] rowHeights
rowHeights に行数より多くの要素がある場合、rowHeights の要素数に合わせるために、グリッドバッグに行が追加されます。
getLayoutDimensions()public double[] columnWeights
null ではない場合、すべての列のウェイトが計算された後で、その値がグリッドバッグに適用されます。columnWeights[i] > が列 i のウェイトより大きい場合、列 i に columnWeights[i] のウェイトが割り当てられます。columnWeights に列数より多くの要素がある場合、超過した要素は無視され、列の作成は行われません。
public double[] rowWeights
rowWeights[i] > が行 i のウェイトより大きい場合、行 i に rowWeights[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 - 照会されるコンポーネント
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 が左から右方向のコンテナのレイアウトの左にある場合、または右から左方向のコンテナのレイアウトの右にある場合、列インデックスはゼロが返されます。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 が GridBagConstraint でない場合public void removeLayoutComponent(Component comp)
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
LayoutManager 内の removeLayoutComponentcomp - 削除されるコンポーネントContainer.remove(java.awt.Component),
Container.removeAll()public Dimension preferredLayoutSize(Container parent)
target コンテナの推奨サイズを指定します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
LayoutManager 内の preferredLayoutSizeparent - 配置されるコンテナContainer.getPreferredSize()public Dimension minimumLayoutSize(Container parent)
target コンテナの最小サイズを指定します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
LayoutManager 内の minimumLayoutSizeparent - 配置されるコンポーネントContainer.doLayout()public Dimension maximumLayoutSize(Container target)
LayoutManager2 内の maximumLayoutSizetarget - レイアウトする必要があるコンポーネントContainer,
minimumLayoutSize(Container),
preferredLayoutSize(Container)public float getLayoutAlignmentX(Container parent)
LayoutManager2 内の getLayoutAlignmentXpublic float getLayoutAlignmentY(Container parent)
LayoutManager2 内の getLayoutAlignmentYpublic void invalidateLayout(Container target)
LayoutManager2 内の invalidateLayoutpublic void layoutContainer(Container parent)
GridBagLayout オブジェクトの制約を満たすために、指定されたコンテナでコンポーネントを再成形します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
LayoutManager 内の layoutContainerparent - 配置が行われるコンテナContainer,
Container.doLayout()public String toString()
Object 内の toString
protected java.awt.GridBagLayoutInfo getLayoutInfo(Container parent,
int sizeflag)
protected java.awt.GridBagLayoutInfo GetLayoutInfo(Container parent,
int sizeflag)
getLayoutInfo
protected void adjustForGravity(GridBagConstraints constraints,
Rectangle r)
protected void AdjustForGravity(GridBagConstraints constraints,
Rectangle r)
adjustForGravity
protected Dimension getMinSize(Container parent,
java.awt.GridBagLayoutInfo info)
protected Dimension GetMinSize(Container parent,
java.awt.GridBagLayoutInfo info)
getMinSizeprotected void arrangeGrid(Container parent)
protected void ArrangeGrid(Container parent)
arrangeGrid
|
JavaTM 2 Platform Std. Ed. v1.4.0 |
||||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | ||||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | ||||||||||
Java、Java 2D、および JDBC は米国ならびにその他の国における米国 Sun Microsystems, Inc. の商標もしくは登録商標です。
Copyright 1993-2002 Sun Microsystems, Inc. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All Rights Reserved.