解答例 - 実習課題2 - 2.基本的なコンポーネント
(実習課題2)
サンプルプログラムを改良しなさい。
- ボタンにアイコンを含ませる事。
- ボタンのサイズをアイコンのちょうど2倍になるようにする事。
- (ヒント)「setPreferredSize」メソッドを使用する。
解答例
package com.techscore.ui.chapter2.exercise2;
/**
* ButtonFrame.java
* TECHSCORE Javaユーザインタフェース2章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ButtonFrame extends JFrame {
public ButtonFrame() {
super("ButtonFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel label = new JLabel("This is a label");
getContentPane().add(label, BorderLayout.CENTER);
ImageIcon icon = new ImageIcon("com/techscore/ui/chapter2/exercise2/4dd.gif");
JButton button = new JButton(icon);
button.setPreferredSize(new Dimension(2 * icon.getIconWidth(), 2 * icon.getIconHeight()));
getContentPane().add(button, BorderLayout.SOUTH);
pack();
}
public static void main(String args[]) {
new ButtonFrame().setVisible(true);
}
}

