|
Java プラットフォーム 1.2 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
java.lang.Object | +--java.awt.GridBagLayout
GridBagLayout
クラスは、異なる大きさのコンポーネントでも縦横に配置させられる柔軟なレイアウトマネージャです。各 GridBagLayout
オブジェクトは、セルによって構成される動的な矩形グリッドを格納しています。各コンポーネントは、1 つまたは複数のセル (表示領域と呼ぶ) を占有します。
ある 1 つのグリッドバッグレイアウトによって管理される各コンポーネントは、表示領域内でのコンポーネントの配置方法を定める GridBagConstraints
のインスタンスに関連付けられます。
GridBagLayout
オブジェクトによるコンポーネントの配置方法は、各コンポーネントに関連付けられている GridBagConstraints
オブジェクトと、コンポーネントのコンテナの最小サイズと推奨サイズによって決定されます。
グリッドバッグレイアウトを効果的に利用するには、コンポーネントの少なくとも 1 つの GridBagConstraints
オブジェクトをカスタマイズする必要があります。GridBagConstraints
オブジェクトをカスタマイズするには、1 つまたは複数のインスタンス変数を設定します。
GridBagConstraints.gridx
,
GridBagConstraints.gridy
gridx = 0
,gridy = 0
のアドレスを持ちます。
そのコンポーネントの直前にコンテナに追加されたコンポーネントのすぐ右 (gridx
) またはすぐ下 (gridy
) に配置する場合は、GridBagConstraints.RELATIVE
(既定値) を設定します。
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
GridBagConstraints.CENTER
(デフォルト)、GridBagConstraints.NORTH
、GridBagConstraints.NORTHEAST
、GridBagConstraints.EAST
、GridBagConstraints.SOUTHEAST
、GridBagConstraints.SOUTH
、GridBagConstraints.SOUTHWEST
、GridBagConstraints.WEST
、GridBagConstraints.NORTHWEST
です。
GridBagConstraints.weightx
,
GridBagConstraints.weighty
weightx
) または 1 列 (weighty
) につき少なくとも 1 つのコンポーネントに重み付け (ウェイト) を設定しないかぎり、すべてのコンポーネントがコンテナの中央に集まってしまうことになります。これは、重み (ウェイト) が 0 の場合 (デフォルト) には、GridBagLayout
オブジェクトが空きスペースをすべてセルのグリッドとコンテナの端の間に配置してしまうからです。
以下の図は、グリッドバッグレイアウトによって管理される 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
, 直列化された形式フィールドの概要 | |
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)
指定された制約オブジェクトを使って、指定されたコンポーネントをレイアウトに追加します。 |
void |
addLayoutComponent(String name,
Component comp)
指定された名前を持つ指定のコンポーネントをレイアウトに追加します。 |
protected void |
AdjustForGravity(GridBagConstraints constraints,
Rectangle r)
|
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)
レイアウト制約を出力します。 |
Point |
getLayoutOrigin()
レイアウトグリッドの原点を指定します。 |
double[][] |
getLayoutWeights()
レイアウトグリッドの列と行のウェイトを指定します。 |
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 comptable
GridBagConstraints
protected GridBagConstraints defaultConstraints
defaultConstraints
のコピーが割り当てられます。getConstraints()
,
setConstraints()
,
lookupConstraints()
protected java.awt.GridBagLayoutInfo layoutInfo
layoutInfo
が null
の場合は、グリッドバッグにコンポーネントがないか、またはコンポーネントがあってもそれがまだ検査を受けていないことを意味します。GetLayoutInfo()
public int[] columnWidths
getLayoutDimensions()
public int[] rowHeights
getLayoutDimensions()
public double[] columnWeights
public double[] 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()
public int[][] getLayoutDimensions()
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
public double[][] getLayoutWeights()
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
public Point location(int x, int y)
(x, y)
で指定される座標を格納するかを判定します。各セルは列インデックス (0 〜 列数 - 1) と行インデックス (0 〜 行数 - 1) で識別されます。
点 (x, y)
がグリッドの外にある場合は、次の規則が適用されます。列インデックスは x
がレイアウトの左側にある場合はゼロとして、右側にある場合は列数として返されます。行インデックスは y
がレイアウトの上にある場合はゼロとして、下にある場合は行数として返されます。
x
- 点の x 座標y
- 点の y 座標public void addLayoutComponent(String name, Component comp)
name
- コンポーネントの名前comp
- 追加されるコンポーネントpublic void addLayoutComponent(Component comp, Object constraints)
comp
- 追加されるコンポーネントconstraints
- コンポーネントをレイアウトに追加する方法を指定するオブジェクトpublic void removeLayoutComponent(Component comp)
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
comp
- 削除されるコンポーネントContainer.remove(java.awt.Component)
,
Container.removeAll()
public Dimension preferredLayoutSize(Container parent)
parent
コンテナの推奨サイズを指定します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
parent
- 配置が行われるコンテナContainer.getPreferredSize()
public Dimension minimumLayoutSize(Container parent)
parent
コンテナの最小サイズを指定します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
parent
- 配置が行われるコンテナContainer.doLayout()
public Dimension maximumLayoutSize(Container target)
target
- 配置される必要があるコンポーネントContainer
,
minimumLayoutSize(java.awt.Container)
,
preferredLayoutSize(java.awt.Container)
public float getLayoutAlignmentX(Container parent)
public float getLayoutAlignmentY(Container parent)
public void invalidateLayout(Container target)
public void layoutContainer(Container parent)
GridBagLayout
オブジェクトの制約を満たすために、指定されたコンテナでコンポーネントを再成します。
ほとんどのアプリケーションはこのメソッドを直接には呼び出しません。
parent
- 配置が行われるコンテナContainer
,
Container.doLayout()
public String toString()
protected java.awt.GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag)
protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r)
protected Dimension GetMinSize(Container parent, java.awt.GridBagLayoutInfo info)
protected void ArrangeGrid(Container parent)
|
Java プラットフォーム 1.2 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |