クラス java.awt.GridBagLayout
全パッケージ クラス階層 このパッケージ 前項目 次項目 インデックス
クラス java.awt.GridBagLayout
java.lang.Object
|
+----java.awt.GridBagLayout
- public class GridBagLayout
- extends Object
- インタフェース LayoutManager
GridBagLayout は、異なる大きさのコンポーネントでも縦横に配置させる
柔軟なレイアウトマネージャである。
各 GridBagLayout はセルによって構成される長方形の格子を利用する。
各コンポーネントは 1 つ以上のセルを占有する(表示領域と呼ばれる)。
ある 1 つの GridBagLayout によって管理される各コンポーネントは、表示領域
内でのコンポーネントの配置方法を定める
GridBagConstraints
のインスタンスと結びつけられる。
GridBagLayout によるコンポーネントの配置方法は
コンポーネントのコンテナのサイズだけではなく、各コンポーネントの
最小サイズと GridBagConstraints によって決定される。
GridBagLayout を効果的に利用する為には、少なくとも 1 つのコンポーネントの
GridBagConstraints をカスタマイズする必要がある。
カスタマイズは GridBagConstraints オブジェクトの 1 つ以上のインスタンス変数
を設定することによって行う。
- gridx,
gridy
- コンポーネントの表示領域の左上にあたるセルを指定する。
左上隅にあたるセルは gridx=0, gridy=0 のアドレスを持つ。
そのコンポーネントの直前にコンテナに追加されたコンポーネント
のすぐ右 (gridx) 又は、 すぐ下 (gridy) に配置する場合は
GridBagConstraints.RELATIVE (デフォルト値) を設定する。
- gridwidth,
gridheight
- コンポーネントの表示領域における
1 行 (gridwidth) 又は、 1 列 (gridheight) あたりのセルの数を指定する。
デフォルト値は 1 である。
コンポーネントが行 (gridwidth) 又は、列 (gridheight) の最後である
ことを指定する場合は GridBagConstraints.REMAINDER を設定する。
次のコンポーネントで行 (gridwidth) 又は、列 (gridheight)が終了する
ことを指定する場合は GridBagConstraints.RELATIVE を設定する。
- fill
- コンポーネントの表示領域が必要とするサイズよりも大きい場合に
コンポーネントのリサイズの方法を指定する。
指定できる値は
GridBagConstraint.NONE
(デフォルト)、
GridBagConstraint.HORIZONTAL
(コンポーネントの高さは変更せずに、幅を表示領域いっぱいにする)、
GridBagConstraint.VERTICAL
(コンポーネントの幅は変更せずに、高さを表示領域いっぱいにする)、
GridBagConstraint.BOTH
(コンポーネントを表示領域いっぱいにする)。
- ipadx,
ipady
- 内側のパディング(コンポーネントの最小サイズに追加する値)を指定する。
コンポーネントの幅は、少なくとも 最小の幅 + ipadx*2 となる
(コンポーネントの両側にパディングされるため)。
同様に、コンポーネントの高さは、少なくとも 最小の高さ + ipady*2
となる。
- insets
- コンポーネントの外側のパディング(コンポーネントと表示領域の端との間の
最小の空間)を指定する。
- anchor
- コンポーネントが表示領域よりも小さい場合に、(表示領域内の)どこに
コンポーネントを配置するのかを指定する。
指定できる値は
GridBagConstraints.CENTER (デフォルト)、
GridBagConstraints.NORTH、
GridBagConstraints.NORTHEAST、
GridBagConstraints.EAST、
GridBagConstraints.SOUTHEAST、
GridBagConstraints.SOUTH、
GridBagConstraints.SOUTHWEST、
GridBagConstraints.WEST、
GridBagConstraints.NORTHWEST。
- weightx,
weighty
- スペースの分配方法を指定する。これはリサイズの際の振る舞を
特定する為に重要である。
1 行 (weightx) 、又は 1 列 (weighty) につき少なくとも 1 つの
コンポーネントに 重み付け(weight) を設定しなくては、全ての
コンポーネントがコンテナの中央に集まってしまうことになる。
これは、重み(weight)が 0 の場合 (デフォルト)には、GridBagLayout
が全ての空き領域をセルの格子とコンテナの端の間に配置してしまう
からである。
以下の図は、GridBagLayout によって管理される 10 個のコンポーネント
(全てボタン) を示す:
全てのコンポーネントは fill=GridBagConstraints.BOTH に設定されている。
さらに、コンポーネントは以下のデフォルトとは異なる制約を持っている:
- Button1, Button2, Button3:
weightx=1.0
- Button4:
weightx=1.0,
gridwidth=GridBagConstraints.REMAINDER
- Button5:
gridwidth=GridBagConstraints.REMAINDER
- Button6:
gridwidth=GridBagConstraints.RELATIVE
- Button7:
gridwidth=GridBagConstraints.REMAINDER
- Button8:
gridheight=2, weighty=1.0,
- Button9, Button 10:
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);
resize(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.resize(f.preferredSize());
f.show();
}
}
-
MAXGRIDSIZE
-
-
MINSIZE
-
-
PREFERREDSIZE
-
-
columnWeights
-
-
columnWidths
-
-
comptable
-
-
defaultConstraints
-
-
layoutInfo
-
-
rowHeights
-
-
rowWeights
-
-
GridBagLayout()
- グリッドバッグレイアウトを作成する。
-
AdjustForGravity(GridBagConstraints, Rectangle)
-
-
ArrangeGrid(Container)
-
-
DumpConstraints(GridBagConstraints)
- レイアウトの制約をプリントする。
-
DumpLayoutInfo(GridBagLayoutInfo)
- レイアウト情報をプリントする。
-
GetLayoutInfo(Container, int)
-
-
GetMinSize(Container, GridBagLayoutInfo)
-
-
addLayoutComponent(String, Component)
- 指定されたコンポーネントに名前を付けてレイアウトに追加する。
-
getConstraints(Component)
- 指定されたコンポーネントの制約を得る。
-
getLayoutDimensions()
-
-
getLayoutOrigin()
-
-
getLayoutWeights()
-
-
layoutContainer(Container)
-
指定されたパネル内のコンテナを配置する。
-
location(int, int)
-
-
lookupConstraints(Component)
- 指定されたコンポーネントの制約を得る。
-
minimumLayoutSize(Container)
- 指定されたパネルのコンポーネントに対するレイアウトの最小サイズ
を返す。
-
preferredLayoutSize(Container)
-
指定されたパネルのコンポーネントに対するレイアウトの推奨サイズ
を返す。
-
removeLayoutComponent(Component)
- 指定されたコンポーネントをレイアウトから削除する。
-
setConstraints(Component, GridBagConstraints)
- 指定されたコンポーネントに対して制約を設定する。
-
toString()
- GridLayout の文字列表現を返す。
MAXGRIDSIZE
protected final static int MAXGRIDSIZE
MINSIZE
protected final static int MINSIZE
PREFERREDSIZE
protected final static int PREFERREDSIZE
comptable
protected Hashtable comptable
defaultConstraints
protected GridBagConstraints defaultConstraints
layoutInfo
protected GridBagLayoutInfo layoutInfo
columnWidths
public int columnWidths[]
rowHeights
public int rowHeights[]
columnWeights
public double columnWeights[]
rowWeights
public double rowWeights[]
GridBagLayout
public GridBagLayout()
- グリッドバッグレイアウトを作成する。
setConstraints
public void setConstraints(Component comp,
GridBagConstraints constraints)
- 指定されたコンポーネントに対して制約を設定する。
- パラメータ:
- comp - 変更されるコンポーネント
- constraints - 適応される制約
getConstraints
public GridBagConstraints getConstraints(Component comp)
- 指定されたコンポーネントの制約を得る。制約のコピーが返される。
- パラメータ:
- comp - 問い合わせを受けるコンポーネント
lookupConstraints
protected GridBagConstraints lookupConstraints(Component comp)
- 指定されたコンポーネントの制約を得る。返されるのはコピーではなく、
レイアウトメカニズムによって実際に使用されている制約クラスである。
- パラメータ:
- comp - 問い合わせを受けるコンポーネント
getLayoutOrigin
public Point getLayoutOrigin()
getLayoutDimensions
public int[][] getLayoutDimensions()
getLayoutWeights
public double[][] getLayoutWeights()
location
public Point location(int x,
int y)
addLayoutComponent
public void addLayoutComponent(String name,
Component comp)
- 指定されたコンポーネントに名前を付けてレイアウトに追加する。
- パラメータ:
- name - コンポーネントの名前
- comp - 追加するコンポーネント
removeLayoutComponent
public void removeLayoutComponent(Component comp)
- 指定されたコンポーネントをレイアウトから削除する。
- パラメータ:
- comp - 削除するコンポーネント
preferredLayoutSize
public Dimension preferredLayoutSize(Container parent)
- 指定されたパネルのコンポーネントに対するレイアウトの推奨サイズ
を返す。
- パラメータ:
- parent - 配置を行うコンポーネント
- 参照:
- minimumLayoutSize
minimumLayoutSize
public Dimension minimumLayoutSize(Container parent)
- 指定されたパネルのコンポーネントに対するレイアウトの最小サイズ
を返す。
- パラメータ:
- parent - 配置を行うコンポーネント
- 参照:
- preferredLayoutSize
layoutContainer
public void layoutContainer(Container parent)
- 指定されたパネル内のコンテナを配置する。
- パラメータ:
- parent - 配置を行うコンポーネント
- 参照:
- Container
toString
public String toString()
- GridLayout の文字列表現を返す。
- オーバーライド:
- クラス Object の toString
DumpLayoutInfo
protected void DumpLayoutInfo(GridBagLayoutInfo s)
- レイアウト情報をプリントする。デバッグの際に有効。
DumpConstraints
protected void DumpConstraints(GridBagConstraints constraints)
- レイアウトの制約をプリントする。デバッグの際に有効。
GetLayoutInfo
protected GridBagLayoutInfo GetLayoutInfo(Container parent,
int sizeflag)
AdjustForGravity
protected void AdjustForGravity(GridBagConstraints constraints,
Rectangle r)
GetMinSize
protected Dimension GetMinSize(Container parent,
GridBagLayoutInfo info)
ArrangeGrid
protected void ArrangeGrid(Container parent)
全パッケージ クラス階層 このパッケージ 前項目 次項目 インデックス
本マニュアルに関する著作権および商標