ダイアログ

import java.awt.*;
import java.awt.event.*;

public class Test {
	public static void main (String[] args)
	{
		Win win = new Win("Test Window");
	}
}

/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends Frame implements ActionListener {

	TextArea ta;
	Dialogbox db;
	String str = new String("");

	/******************/
	/* コンストラクタ */
	/******************/
	Win (String name)
	{
					// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
		super(name);
					// テキストエリアの追加
		Font f = new Font("MS 明朝", Font.PLAIN, 20);
		ta = new TextArea(4, 25);
		ta.setFont(f);
		add(ta, BorderLayout.NORTH);
					// イベントアダプタ
		addMouseListener(new PressMouse());
		addWindowListener(new WinEnd());
					// Windowの大きさ
		setSize(300, 300);
					// Windowの位置(中央)
		Toolkit tool = getToolkit();
		Dimension d = tool.getScreenSize();   // ディスプレイの大きさ
		setLocation(d.width / 2 - 150, d.height / 2 - 150);
					// ウィンドウを表示
		setVisible(true);
					// Dialogboxの定義
		db = new Dialogbox(this);
					// ActionListener
		db.bt1.addActionListener(this);
		db.bt2.addActionListener(this);
		db.bt3.addActionListener(this);
	}

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

	/****************************************/
	/* マウスの右ボタンが押されたときの処理 */
	/****************************************/
	class PressMouse extends MouseAdapter {
		public void mousePressed(MouseEvent e)
		{
			if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {

				if (ta.getSelectedText().length() == 0)
					db.bt1.setEnabled(false);
				else
					db.bt1.setEnabled(true);

				if (ta.getSelectedText().length() == 0)
					db.bt2.setEnabled(false);
				else
					db.bt2.setEnabled(true);

				if (str.length() == 0)
					db.bt3.setEnabled(false);
				else
					db.bt3.setEnabled(true);

				db.setVisible(true);
			}
		}
	}

	/******************************************/
	/* ダイアログのボタンが押されたときの処理 */
	/******************************************/
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == db.bt1) {
			str = ta.getSelectedText();
			db.setVisible(false);
		}
		if (e.getSource() == db.bt2) {
			str = ta.getSelectedText();
			int k1 = ta.getSelectionStart();
			int k2 = ta.getSelectionEnd();
			ta.replaceRange("", k1, k2);
			db.setVisible(false);
		}
		if (e.getSource() == db.bt3) {
			int k3 = ta.getCaretPosition();
			ta.insert(str, k3);
			db.setVisible(false);
		}
	}

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

/*******************/
/* Dialogboxクラス */
/*******************/
class Dialogbox extends Dialog {
	Button bt1, bt2, bt3;
					// コンストラクタ
	Dialogbox(Frame host)
	{
		super(host, "Dialog", true);

		setLayout(new GridLayout(3, 1, 5, 0));
		Font f = new Font("MS 明朝", Font.PLAIN, 20);
		setSize(100, 120);

		Toolkit tool = getToolkit();
		Dimension d = tool.getScreenSize();   // ディスプレイの大きさ
		setLocation(d.width / 2 - 350, d.height / 2 - 150);

		bt1 = new Button("コピー");
		bt1.setFont(f);
		add(bt1);

		bt2 = new Button("切り取り");
		bt2.setFont(f);
		add(bt2);

		bt3 = new Button("貼り付け");
		bt3.setFont(f);
		add(bt3);

		addWindowListener(new WinEnd());
	}

	/************/
	/* 終了処理 */
	/************/
	class WinEnd extends WindowAdapter
	{
		public void windowClosing(WindowEvent e) {
			setVisible(false);
		}
	}
}