|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||
java.lang.Objectjava.awt.GridBagLayout
public class GridBagLayout
GridBagLayout クラスは、異なる大きさのコンポーネントでも縦横に配置できる柔軟なレイアウトマネージャです。それぞれの GridBagLayout オブジェクトは、セルによって構成される動的な矩形グリッドを格納しています。各コンポーネントは、1 つ以上のセル (「表示領域」と呼ぶ) に配置されます。
GridBagLayout によって管理される各コンポーネントは、GridBagConstraints のインスタンスと関連します。制約オブジェクトは、コンポーネントの表示領域をグリッド上に配置する場所、およびコンポーネントをその表示領域内に配置する方法を指定します。また、この制約オブジェクトに加え、GridBagLayout は、コンポーネントのサイズを決定するために、各コンポーネントの最小サイズおよび推奨サイズを考慮します。
グリッドの全体の方向は、コンテナの ComponentOrientation プロパティによって決まります。水平方向に左から右の場合、グリッドの座標 (0, 0) はコンテナの左上隅になります。この場合、X 座標は右方向に、Y 座標は下方向に値が増加します。水平方向に右から左の場合、グリッドの座標 (0, 0) はコンテナの右上隅になります。この場合、X 座標は左方向に、Y 座標は下方向に値が増加します。
グリッドバッグレイアウトを効果的に利用するには、コンポーネントの少なくとも 1 つの GridBagConstraints オブジェクトをカスタマイズする必要があります。GridBagConstraints オブジェクトをカスタマイズするには、1 つまたは複数のインスタンス変数を設定します。
GridBagConstraints.gridx、GridBagConstraints.gridygridx = 0、gridy = 0 です。水平方向に左から右へのレイアウトの場合、コンポーネントのリーディングコーナーは左上隅です。水平方向に右から左へのレイアウトの場合、コンポーネントのリーディングコーナーは右上隅です。GridBagConstraints.RELATIVE (規定値) を使用して、コンポーネントを、このコンポーネントが追加される直前にコンテナに追加されたコンポーネントの直後に (gridx は x 軸に沿って、gridy は y 軸に沿って) 配置するよう指定します。
GridBagConstraints.gridwidth、GridBagConstraints.gridheightgridwidth) または 1 列 (gridheight) あたりのセルの数を指定します。既定値は 1 です。コンポーネントの表示領域が gridx から行の最後のセルまで (gridwidth) または gridy から列の最後のセルまで (gridheight) であることを指定する場合は、GridBagConstraints.REMAINDER を使用します。
コンポーネントの表示領域が gridx から、行の最後から 2 番目のセルまで (gridwidth)、または gridy から、列の最後から 2 番目のセルまで (gridheight) であることを指定する場合は、GridBagConstraints.RELATIVE を使用します。
GridBagConstraints.fillGridBagConstraints.NONE (デフォルト)、GridBagConstraints.HORIZONTAL (コンポーネントの高さは変更せずに、幅を表示領域最大にする)、GridBagConstraints.VERTICAL (コンポーネントの幅は変更せずに、高さを表示領域最大にする)、GridBagConstraints.BOTH (コンポーネントを表示領域最大にする) です。
GridBagConstraints.ipadx、GridBagConstraints.ipadyipadx ピクセルとなります。同様に、コンポーネントの高さは、少なくとも最小の高さ + ipady ピクセルとなります。
GridBagConstraints.insetsGridBagConstraints.anchorComponentOrientation プロパティを基準にして解釈されます。有効な値は次のとおりです。
絶対値 | 相対値 |
|---|---|
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.weightyweightx) または 1 列 (weighty) につき、少なくとも 1 つのコンポーネントにウェイトを設定しないかぎり、すべてのコンポーネントがコンテナの中央に集まります。これは、ウェイトが 0 の場合 (デフォルト) には、GridBagLayout オブジェクトが余分なスペースをすべてセルのグリッドとコンテナの端の間に配置してしまうからです。 次に示す図は、グリッドバッグレイアウトによって管理される 10 個のコンポーネント (すべてのボタン) を示します。図 1 は水平方向に左から右へのコンテナを示し、図 2 は水平方向に右から左へのコンテナを示します。
| |
| 図 1: 水平方向に左から右 | 図 2: 水平方向に右から左 |
10 個のコンポーネントのそれぞれは、その関連している GridBagConstraints オブジェクトの fill フィールドが GridBagConstraints.BOTH に設定されています。さらにコンポーネントは、デフォルトとは異なる以下の制約を持っています。
weightx = 1.0weightx = 1.0、gridwidth = GridBagConstraints.REMAINDERgridwidth = GridBagConstraints.REMAINDERgridwidth = GridBagConstraints.RELATIVEgridwidth = GridBagConstraints.REMAINDERgridheight = 2、weighty = 1.0gridwidth = 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);
.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<Component,GridBagConstraints> |
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)
管理される子の現在のセットに対する GridBagLayoutInfo のインスタンスに値を入れます。 |
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)
このグリッドバッグレイアウトを使って、 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 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 が 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 が左から右方向のコンテナのレイアウトの左にある場合、または右から左方向のコンテナのレイアウトの右にある場合、列インデックスはゼロが返されます。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)
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 java.awt.GridBagLayoutInfo getLayoutInfo(Container parent,
int sizeflag)
GridBagLayoutInfo のインスタンスに値を入れます。このインスタンスには、子のセットを通じて、次の 3 つの段階の実行が必要です。
このメソッドは GridBagLayout によって内部でのみ使用されます。
parent - レイアウトコンテナsizeflag - PREFERREDSIZE または MINSIZE のどちらか
GridBagLayoutInfo
protected java.awt.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,
java.awt.GridBagLayoutInfo info)
getLayoutInfo からの情報に基づいて、マスターの最小サイズを算出します。このメソッドは GridBagLayout によって内部でのみ使用されます。
parent - レイアウトコンテナinfo - この親のレイアウト情報
Dimension オブジェクト
protected Dimension GetMinSize(Container parent,
java.awt.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. 5.0 |
|||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||
Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。