JButton クラス(AbstractAction)

( 2 つのプログラム例を含む)

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", "Test Data");
	}
}

/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends JFrame {

	JButton bt1, bt2;
	JTextArea tx;

	/******************/
	/* コンストラクタ */
	/******************/
	Win (String name, String data)
	{
					// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
		super(name);
					// レイアウトの変更(行,列,水平ギャップ,垂直ギャップ)
		Container cp = getContentPane();
		cp.setLayout(new GridLayout(2, 1, 5, 10));
					// 上のパネル
						// パネルの追加
		JPanel pn1 = new JPanel();
		cp.add(pn1);
						// アクションの定義
		Action red = new AbstractAction()
		{
			public void actionPerformed(ActionEvent e)
			{
				tx.setForeground(Color.red);
			}
		};

		Action blue = new AbstractAction()
		{
			public void actionPerformed(ActionEvent e)
			{
				tx.setForeground(Color.blue);
			}
		};
						// ボタン1の追加
		bt1 = new JButton(red);
		bt1.setIcon(new ImageIcon("red.gif"));
		bt1.setToolTipText("文字を赤くします");
		pn1.add(bt1);
						// ボタン2の追加
		bt2 = new JButton(blue);
		bt2.setIcon(new ImageIcon("blue.gif"));
		bt2.setToolTipText("文字を青くします");
		pn1.add(bt2);
					// 下のパネル
		Font f = new Font("MS 明朝", Font.BOLD, 20);
						// パネルの追加
		JPanel pn2 = new JPanel();
		cp.add(pn2);
						// テキストエリアの追加
		tx = new JTextArea("Text Area", 5, 25);
		tx.setFont(f);
		pn2.add(tx);
					// Windowの大きさ
		setSize(300, 300);
					// ウィンドウを表示
		setVisible(true);
					// イベントアダプタ
		addWindowListener(new WinEnd());
	}

	/******************************/
	/* 上,左,下,右の余白の設定 */
	/******************************/
	public Insets getInsets()
	{
		return new Insets(35, 10, 10, 10);
	}

	/************/
	/* 終了処理 */
	/************/
	class WinEnd extends WindowAdapter
	{
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
}

// 上のプログラムは,以下のようにも書ける

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", "Test Data");
	}
}

/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends JFrame {

	JButton bt1, bt2;
	JTextArea tx;

	/******************/
	/* コンストラクタ */
	/******************/
	Win (String name, String data)
	{
					// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
		super(name);
					// レイアウトの変更(行,列,水平ギャップ,垂直ギャップ)
		Container cp = getContentPane();
		cp.setLayout(new GridLayout(2, 1, 5, 10));
					// 上のパネル
						// パネルの追加
		JPanel pn1 = new JPanel();
		cp.add(pn1);
						// アクションの定義
		Action red = new AbstractAction("文字を赤くします", new ImageIcon("red.gif"))
		{
			public void actionPerformed(ActionEvent e)
			{
				tx.setForeground(Color.red);
			}
		};

		Action blue = new AbstractAction("文字を青くします", new ImageIcon("blue.gif"))
		{
			public void actionPerformed(ActionEvent e)
			{
				tx.setForeground(Color.blue);
			}
		};
						// ボタン1の追加
		bt1 = new Botan(red);
		pn1.add(bt1);
						// ボタン2の追加
		bt2 = new Botan(blue);
		pn1.add(bt2);
					// 下のパネル
		Font f = new Font("MS 明朝", Font.BOLD, 20);
						// パネルの追加
		JPanel pn2 = new JPanel();
		cp.add(pn2);
						// テキストエリアの追加
		tx = new JTextArea("Text Area", 5, 25);
		tx.setFont(f);
		pn2.add(tx);
					// Windowの大きさ
		setSize(300, 300);
					// ウィンドウを表示
		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);
		}
	}
}