タイトル
 メニューにないコーナーはTopからいけます
TOPJavaSwingJOptionPane → This Page

JOptionPane@Swing サンプル06

概要

Java - Swing - JOptionPane のサンプルです。
・色々な入力ダイアログ

解説

入力ダイアログ1は親を指定せずメッセージを指定、
入力ダイアログ2は親を指定してメッセージを指定、
入力ダイアログ3は親を指定せずメッセージと初期値を指定、
入力ダイアログ4は親を指定してメッセージと初期値を指定、
入力ダイアログ5は親を指定してメッセージとタイトルを指定、
入力ダイアログ6は親を指定してメッセージとタイトルを指定し、入力内容を選択式にしています。
それぞれ親を指定すると親ウインドウを中心にダイアログが表示されます。

サンプルイメージ

メイン画面
サンプル画像

入力ダイアログ1
サンプル画像

入力ダイアログ2
サンプル画像

入力ダイアログ3
サンプル画像

入力ダイアログ4
サンプル画像

入力ダイアログ5
サンプル画像

入力ダイアログ6
サンプル画像


サンプルソース

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 * JOptionPane サンプル06
 * ・色々な入力ダイアログ
 * 
 * @author みっちー
 */
public class JOptionPane06 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JLabel label;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JOptionPane06 frame = new JOptionPane06();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JOptionPane サンプル06");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 470, 150);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JOptionPane06() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// ボタンを作成
		JButton button1 = new JButton("input1");
		JButton button2 = new JButton("input2");
		JButton button3 = new JButton("input3");
		JButton button4 = new JButton("input4");
		JButton button5 = new JButton("input5");
		JButton button6 = new JButton("input6");
		
		// アクションコマンドを設定
		button1.setActionCommand("input1");
		button2.setActionCommand("input2");
		button3.setActionCommand("input3");
		button4.setActionCommand("input4");
		button5.setActionCommand("input5");
		button6.setActionCommand("input6");
		
		// アクションリスナー追加
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		button5.addActionListener(this);
		button6.addActionListener(this);
		
		// ラベルを生成
		label = new JLabel();
		label.setPreferredSize(new Dimension(100, 30));
		label.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		// ボタン、ラベルを追加
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(button4);
		panelBase.add(button5);
		panelBase.add(button6);
		panelBase.add(label);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		String ret = "";
		if ("input1".equals(e.getActionCommand())) {
			ret = JOptionPane.showInputDialog("input1");
		} else if ("input2".equals(e.getActionCommand())) {
			ret = JOptionPane.showInputDialog(this, "input2");
		} else if ("input3".equals(e.getActionCommand())) {
			ret = JOptionPane.showInputDialog("input3", "初期値3");
		} else if ("input4".equals(e.getActionCommand())) {
			ret = JOptionPane.showInputDialog(this, "input4", "初期値4");
		} else if ("input5".equals(e.getActionCommand())) {
			ret = JOptionPane.showInputDialog(this, "input5", "title5", JOptionPane.PLAIN_MESSAGE);
		} else if ("input6".equals(e.getActionCommand())) {
			String options[] = { "aaaaa", "bbbbb", "ccccc", "ddddd" };
			ret = (String) JOptionPane.showInputDialog(this, "input6", "title6", JOptionPane.PLAIN_MESSAGE, null, options, options[2]);
		}
		label.setText(ret);
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/08/27 新規作成


TOPJavaSwingJOptionPane → This Page
Valid CSS!