クラス 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 に設定されている。 さらに、コンポーネントは以下のデフォルトとは異なる制約を持っている: 上記の例を実装したサンプルコードを示す:
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();
    }
}
    

Variable Index

 o MAXGRIDSIZE
 o MINSIZE
 o PREFERREDSIZE
 o columnWeights
 o columnWidths
 o comptable
 o defaultConstraints
 o layoutInfo
 o rowHeights
 o rowWeights

Constructor Index

 o GridBagLayout()
グリッドバッグレイアウトを作成する。

Method Index

 o AdjustForGravity(GridBagConstraints, Rectangle)
 o ArrangeGrid(Container)
 o DumpConstraints(GridBagConstraints)
レイアウトの制約をプリントする。
 o DumpLayoutInfo(GridBagLayoutInfo)
レイアウト情報をプリントする。
 o GetLayoutInfo(Container, int)
 o GetMinSize(Container, GridBagLayoutInfo)
 o addLayoutComponent(String, Component)
指定されたコンポーネントに名前を付けてレイアウトに追加する。
 o getConstraints(Component)
指定されたコンポーネントの制約を得る。
 o getLayoutDimensions()
 o getLayoutOrigin()
 o getLayoutWeights()
 o layoutContainer(Container)
指定されたパネル内のコンテナを配置する。
 o location(int, int)
 o lookupConstraints(Component)
指定されたコンポーネントの制約を得る。
 o minimumLayoutSize(Container)
指定されたパネルのコンポーネントに対するレイアウトの最小サイズ を返す。
 o preferredLayoutSize(Container)
指定されたパネルのコンポーネントに対するレイアウトの推奨サイズ を返す。
 o removeLayoutComponent(Component)
指定されたコンポーネントをレイアウトから削除する。
 o setConstraints(Component, GridBagConstraints)
指定されたコンポーネントに対して制約を設定する。
 o toString()
GridLayout の文字列表現を返す。

Variables

 o MAXGRIDSIZE
  protected final static int MAXGRIDSIZE
 o MINSIZE
  protected final static int MINSIZE
 o PREFERREDSIZE
  protected final static int PREFERREDSIZE
 o comptable
  protected Hashtable comptable
 o defaultConstraints
  protected GridBagConstraints defaultConstraints
 o layoutInfo
  protected GridBagLayoutInfo layoutInfo
 o columnWidths
  public int columnWidths[]
 o rowHeights
  public int rowHeights[]
 o columnWeights
  public double columnWeights[]
 o rowWeights
  public double rowWeights[]

Constructors

 o GridBagLayout
  public GridBagLayout()
グリッドバッグレイアウトを作成する。

Methods

 o setConstraints
  public void setConstraints(Component comp,
                             GridBagConstraints constraints)
指定されたコンポーネントに対して制約を設定する。
パラメータ:
comp - 変更されるコンポーネント
constraints - 適応される制約
 o getConstraints
  public GridBagConstraints getConstraints(Component comp)
指定されたコンポーネントの制約を得る。制約のコピーが返される。
パラメータ:
comp - 問い合わせを受けるコンポーネント
 o lookupConstraints
  protected GridBagConstraints lookupConstraints(Component comp)
指定されたコンポーネントの制約を得る。返されるのはコピーではなく、 レイアウトメカニズムによって実際に使用されている制約クラスである。
パラメータ:
comp - 問い合わせを受けるコンポーネント
 o getLayoutOrigin
  public Point getLayoutOrigin()
 o getLayoutDimensions
  public int[][] getLayoutDimensions()
 o getLayoutWeights
  public double[][] getLayoutWeights()
 o location
  public Point location(int x,
                        int y)
 o addLayoutComponent
  public void addLayoutComponent(String name,
                                 Component comp)
指定されたコンポーネントに名前を付けてレイアウトに追加する。
パラメータ:
name - コンポーネントの名前
comp - 追加するコンポーネント
 o removeLayoutComponent
  public void removeLayoutComponent(Component comp)
指定されたコンポーネントをレイアウトから削除する。
パラメータ:
comp - 削除するコンポーネント
 o preferredLayoutSize
  public Dimension preferredLayoutSize(Container parent)
指定されたパネルのコンポーネントに対するレイアウトの推奨サイズ を返す。
パラメータ:
parent - 配置を行うコンポーネント
参照:
minimumLayoutSize
 o minimumLayoutSize
  public Dimension minimumLayoutSize(Container parent)
指定されたパネルのコンポーネントに対するレイアウトの最小サイズ を返す。
パラメータ:
parent - 配置を行うコンポーネント
参照:
preferredLayoutSize
 o layoutContainer
  public void layoutContainer(Container parent)
指定されたパネル内のコンテナを配置する。
パラメータ:
parent - 配置を行うコンポーネント
参照:
Container
 o toString
  public String toString()
GridLayout の文字列表現を返す。
オーバーライド:
クラス ObjecttoString
 o DumpLayoutInfo
  protected void DumpLayoutInfo(GridBagLayoutInfo s)
レイアウト情報をプリントする。デバッグの際に有効。
 o DumpConstraints
  protected void DumpConstraints(GridBagConstraints constraints)
レイアウトの制約をプリントする。デバッグの際に有効。
 o GetLayoutInfo
  protected GridBagLayoutInfo GetLayoutInfo(Container parent,
                                            int sizeflag)
 o AdjustForGravity
  protected void AdjustForGravity(GridBagConstraints constraints,
                                  Rectangle r)
 o GetMinSize
  protected Dimension GetMinSize(Container parent,
                                 GridBagLayoutInfo info)
 o ArrangeGrid
  protected void ArrangeGrid(Container parent)

全パッケージ  クラス階層  このパッケージ  前項目  次項目  インデックス

本マニュアルに関する著作権および商標