TOP →
Java →
Swing →
JOptionPane → This Page
JOptionPane@Swing サンプル05
概要
Java -
Swing -
JOptionPane のサンプルです。
・色々な選択肢
・戻り値の取得と判別
解説
optionType によって選択肢が変わります。
実際に選択された内容は戻り値と以下を比較して判別できます。
・JOptionPane.YES_OPTION
・JOptionPane.OK_OPTION
・JOptionPane.NO_OPTION
・JOptionPane.CANCEL_OPTION
サンプルイメージ
メイン画面
JOptionPane.DEFAULT_OPTION
JOptionPane.YES_NO_OPTION
JOptionPane.YES_NO_CANCEL_OPTION
JOptionPane.OK_CANCEL_OPTION
サンプルソース
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 サンプル05
* ・色々な選択肢
* ・戻り値の取得と判別
*
* @author みっちー
*/
public class JOptionPane05 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel label;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JOptionPane05 frame = new JOptionPane05();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JOptionPane サンプル05");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 420, 150);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JOptionPane05() {
// パネルを作成
JPanel panelBase = new JPanel();
// ボタンを作成
JButton button1 = new JButton("Confirm1");
JButton button2 = new JButton("Confirm2");
JButton button3 = new JButton("Confirm3");
JButton button4 = new JButton("Confirm4");
// アクションコマンドを設定
button1.setActionCommand("Confirm1");
button2.setActionCommand("Confirm2");
button3.setActionCommand("Confirm3");
button4.setActionCommand("Confirm4");
// アクションリスナー追加
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.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(label);
// パネルを追加
getContentPane().add(panelBase);
}
/**
* アクション
*/
public void actionPerformed(ActionEvent e) {
int ret = 0;
if ("Confirm1".equals(e.getActionCommand())) {
// OK
ret = JOptionPane.showConfirmDialog(this, "Confirm1", "title1", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE);
} else if ("Confirm2".equals(e.getActionCommand())) {
// はい、いいえ
ret = JOptionPane.showConfirmDialog(this, "Confirm2", "title2", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
} else if ("Confirm3".equals(e.getActionCommand())) {
// はい、いいえ、取消
ret = JOptionPane.showConfirmDialog(this, "Confirm3", "title3", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
} else if ("Confirm4".equals(e.getActionCommand())) {
// OK、取消
ret = JOptionPane.showConfirmDialog(this, "Confirm4", "title4", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
if (ret == JOptionPane.YES_OPTION) {
label.setText("OK, はい");
} else if (ret == JOptionPane.NO_OPTION) {
label.setText("いいえ");
} else {
label.setText("取消");
}
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/08/27 新規作成
TOP →
Java →
Swing →
JOptionPane → This Page