GridBagPanel.java
import java.awt.*;
import java.applet.Applet;
/** ボタンのレイアウトをテストする GridBagPanel クラス */
public class GridBagPanel extends Applet {
/** インターフェイスの部品の宣言 */
public Button buttons[];
/** フォントと色の宣言 */
public Font font;
public Color color;
/** ボタンの個数 */
protected static final int MAX_NUM = 5;
/** レイアウト */
public GridBagLayout lay;
/** レイアウトの細かい条件 */
public GridBagConstraints gbc;
/**
* 初期設定のメソッド
* レイアウトを GridBagPanelに設定
*/
public void init() {
lay = new GridBagLayout(); //レイアウトの生成
setLayout( lay ); //レイアウトの設定
gbc = new GridBagConstraints(); //レイアウトの細かい情報
buttons = new Button[MAX_NUM];
color = new Color( 35, 140, 35 );
font = new Font( "TimesRoman", Font.BOLD, 18 );
for( int i=0; i<MAX_NUM; i++ ) {
buttons[i] = new Button( "Button" + i );
buttons[i].setBackground( color );
buttons[i].setForeground( Color.white );
buttons[i].setFont( font );
}
gbc.fill = GridBagConstraints.BOTH; //縦横埋める
gbc.weightx = 3.0; // 横のサイズの全体を 3 とする。
gbc.weighty = 1.0; // 縦のサイズの全体を 1 とする。
gbc.gridwidth = GridBagConstraints.REMAINDER; //残り全部
lay.setConstraints( buttons[0], gbc );
add( buttons[0] );
gbc.gridheight = 2;
gbc.gridwidth = 1;
lay.setConstraints( buttons[1], gbc );
add( buttons[1] );
gbc.gridheight = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER; //残り全部
lay.setConstraints( buttons[2], gbc );
add( buttons[2] );
gbc.gridwidth = 1;
lay.setConstraints( buttons[3], gbc );
add( buttons[3] );
gbc.gridwidth = GridBagConstraints.REMAINDER; //残り全部
lay.setConstraints( buttons[4], gbc );
add( buttons[4] );
}
}