import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends JFrame { JTextArea ta; /******************/ /* コンストラクタ */ /******************/ Win (String name) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // テキストエリアの追加 Font f = new Font("MS 明朝", Font.PLAIN, 20); Container cp = getContentPane(); ta = new JTextArea(5, 20); ta.setFont(f); cp.add(ta); // ツールバーの追加 // アクションの定義 Action Japan = new AbstractAction("日本", new ImageIcon("Japan.gif")) { public void actionPerformed(ActionEvent e) { ta.setText("日本の国旗です"); } }; Action China = new AbstractAction("中国", new ImageIcon("China.gif")) { public void actionPerformed(ActionEvent e) { ta.setText("中国の国旗です"); } }; Action France = new AbstractAction("フランス", new ImageIcon("France.gif")) { public void actionPerformed(ActionEvent e) { ta.setText("フランスの国旗です"); } }; Action England = new AbstractAction("イギリス", new ImageIcon("England.gif")) { public void actionPerformed(ActionEvent e) { ta.setText("イギリスの国旗です"); } }; Action exit = new AbstractAction("終了", new ImageIcon("exit.gif")) { public void actionPerformed(ActionEvent e) { System.exit(0); } }; // ツールバーの生成 JToolBar bar = new JToolBar(); // ボタンを付加 JButton bt1 = new Botan(Japan); bar.add(bt1); JButton bt2 = new Botan(China); bar.add(bt2); JButton bt3 = new Botan(France); bar.add(bt3); JButton bt4 = new Botan(England); bar.add(bt4); bar.addSeparator(); JButton bt5 = new Botan(exit); bar.add(bt5); cp.add(bar, BorderLayout.NORTH); // Windowの大きさ setSize(300, 150); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /**************************************/ /* ボタンの定義(ツールチップの付加) */ /**************************************/ class Botan extends JButton { public Botan(Action a) { super((Icon)a.getValue(Action.SMALL_ICON)); String tip = (String)a.getValue(Action.SHORT_DESCRIPTION); if (tip == null) tip = (String)a.getValue(Action.NAME); if (tip != null) setToolTipText(tip); addActionListener(a); } } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }