解答例 - 実習課題1 - 5.レイアウト・マネージャー2
(実習課題1)
上記のウィンドウサンプルを表示するプログラムを作成しなさい。
- ウィンドウ内のコンポーネントはボタン(JButton)のみ。
解答例
package com.techscore.ui.chapter5.exercise1;
/**
* GridLayoutFrame.java
* TECHSCORE Javaユーザインタフェース5章 実習課題1
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutFrame extends JFrame {
public GridLayoutFrame() {
super("Grid Layout");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//GridLayoutの作成
getContentPane().setLayout(new GridLayout(2, 2));
//ボタンを4つ配置
JButton first = new JButton("first");
first.setBackground(Color.red);
getContentPane().add(first);
JButton second = new JButton("second");
second.setBackground(Color.yellow);
getContentPane().add(second);
JButton third = new JButton("third");
third.setBackground(Color.blue);
getContentPane().add(third);
JButton forth = new JButton("fourth");
forth.setBackground(Color.green);
getContentPane().add(forth);
pack();
}
public static void main(String args[]) {
new GridLayoutFrame().setVisible(true);
}
}

