解答例 - 実習課題2 - 6.コンテナ
(実習課題2)
以下のプログラムを作成しなさい。
- ウィンドウ(JFrame)に直接配置されるコンポーネントは縦方向の「Box」1つ。
- 「Box」の中にボタンを4つ配置する事。ボタンのテキストなどは任意。
- 各ボタンの間には「glue」をはさむ事。また1番上のボタンの上、と1番下のボタンの下にはサイズ5の縦方向「strut」を入れる事。
ウィンドウのサイズを変化させて、「glue」および「strut」の効果を確認する事。
解答例
/**
* BoxFrame.java
* TECHSCORE Javaユーザインタフェース6章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.ui.chapter6.exercise2;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BoxFrame extends JFrame {
public BoxFrame() {
super("BoxFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Box box = Box.createVerticalBox();
getContentPane().add(box);
box.add(Box.createVerticalStrut(5));
box.add(new JButton("button 1"));
box.add(Box.createGlue());
box.add(new JButton("button 2"));
box.add(Box.createGlue());
box.add(new JButton("button 3"));
box.add(Box.createGlue());
box.add(new JButton("button 4"));
box.add(Box.createVerticalStrut(5));
pack();
}
public static void main(String args[]) {
new BoxFrame().setVisible(true);
}
}

