解答例 - 実習課題2 - 4.レイアウト・マネージャー
(実習課題2)
上のサンプルのウィンドウを表示するプログラムを作成しなさい。
- 使用しているコンポーネントは「JButton」のみ。
- レイアウトマネージャーは「FlowLayout」です。
解答例
package com.techscore.ui.chapter4.exercise2;
/**
* FlowLayoutFrame.java
* TECHSCORE Javaユーザインタフェース4章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutFrame extends JFrame {
public FlowLayoutFrame() {
super("Flow Layout");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
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);
pack();
}
public static void main(String args[]) {
new FlowLayoutFrame().setVisible(true);
}
}

