JOptionPane クラス

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 implements ActionListener {

	JButton bt1, bt2, bt3, bt4, bt5;
	JTextArea tx;

	/******************/
	/* コンストラクタ */
	/******************/
	Win (String name, String data)
	{
					// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
		super(name);
					// レイアウトの変更(行,列,水平ギャップ,垂直ギャップ)
		Container cp = getContentPane();
		cp.setLayout(new GridLayout(2, 1, 5, 10));
		Font f = new Font("MS 明朝", Font.BOLD, 20);
					// 上のパネル
						// パネルの追加
		JPanel pn1 = new JPanel();
		pn1.setLayout(new GridLayout(5, 1, 5, 5));
		cp.add(pn1);
						// ボタンの追加
		bt1 = new JButton("showMessageDialog");
		bt1.addActionListener(this);
		bt1.setFont(f);
		pn1.add(bt1);

		bt2 = new JButton("showConfirmDialog");
		bt2.addActionListener(this);
		bt2.setFont(f);
		pn1.add(bt2);

		bt3 = new JButton("showInputDialog(入力)");
		bt3.addActionListener(this);
		bt3.setFont(f);
		pn1.add(bt3);

		bt4 = new JButton("showInputDialog(選択)");
		bt4.addActionListener(this);
		bt4.setFont(f);
		pn1.add(bt4);

		bt5 = new JButton("showOptionDialog");
		bt5.addActionListener(this);
		bt5.setFont(f);
		pn1.add(bt5);
					// 下のパネル
						// パネルの追加
		JPanel pn2 = new JPanel();
		cp.add(pn2);
						// テキストエリアの追加
		tx = new JTextArea(5, 25);
		tx.setFont(f);
		JScrollPane scroll = new JScrollPane(tx);
		pn2.add(scroll);
					// Windowの大きさ
		setSize(350, 300);
					// ウィンドウを表示
		setVisible(true);
					// イベントアダプタ
		addWindowListener(new WinEnd());
	}

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

	/******************************/
	/* ボタンが押されたときの処理 */
	/******************************/
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == bt1) {
			JOptionPane.showMessageDialog(this, "よろしいですか?");
			tx.setText("確認終了\n");
		}
		else if (e.getSource() == bt2) {
			int k = JOptionPane.showConfirmDialog(this, "よろしいですか?");
			tx.setText("選ばれた項目は\n");
			if (k == JOptionPane.OK_OPTION)
				tx.append("   YES");
			else if (k == JOptionPane.NO_OPTION)
				tx.append("   NO");
			else
				tx.append("   No Answer");
		}
		else if (e.getSource() == bt3) {
			String str = JOptionPane.showInputDialog(this, "文字列を入力してください");
			tx.setText("入力された文字列は\n");
			if (str != null)
				tx.append("   " + str);
			else
				tx.append("   null");
		}
		else if (e.getSource() == bt4) {
			Object str[] = {"桜", "紫陽花", "朝顔"};
			Object k = JOptionPane.showInputDialog(this, "どれかを選んでください",
                       "選択", JOptionPane.QUESTION_MESSAGE, null, str, str[0]);
			tx.setText("選択された文字列は\n");
			if (k != null)
				tx.append("   " + k);
			else
				tx.append("   null");
		}
		else {
			Object str[] = {"桜", "紫陽花", "朝顔", "椿"};
			int k = JOptionPane.showOptionDialog(this, "どれかを選んでください",
                    "選択", JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, str, str[0]);
			tx.setText("選択された文字列は\n");
			if (k >= 0)
				tx.append("   " + str[k]);
			else
				tx.append("   null");
		}
	}

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