TOP →
Java →
Swing →
JOptionPane → This Page
JOptionPane@Swing サンプル01
概要
Java -
Swing -
JOptionPane のサンプルです。
・色々なダイアログ表示
解説
メッセージダイアログ、確認(選択)ダイアログ、入力ダイアログを表示しています。
サンプルイメージ
メイン画面。
メッセージダイアログ
確認(選択)ダイアログ
入力ダイアログ
サンプルソース
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* JOptionPane サンプル01
* ・色々なダイアログ表示
*
* @author みっちー
*/
public class JOptionPane01 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JOptionPane01 frame = new JOptionPane01();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JOptionPane サンプル01");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 400, 100);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JOptionPane01() {
// パネルを作成
JPanel panelBase = new JPanel();
// ボタンを作成
JButton button1 = new JButton("Message");
JButton button2 = new JButton("Confirm");
JButton button3 = new JButton("Input");
// アクションコマンドを設定
button1.setActionCommand("Message");
button2.setActionCommand("Confirm");
button3.setActionCommand("Input");
// アクションリスナー追加
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
// ボタンを追加
panelBase.add(button1);
panelBase.add(button2);
panelBase.add(button3);
// パネルを追加
getContentPane().add(panelBase);
}
/**
* アクション
*/
public void actionPerformed(ActionEvent e) {
if ("Message".equals(e.getActionCommand())) {
// メッセージダイアログ表示
JOptionPane.showMessageDialog(this, "Message");
} else if ("Confirm".equals(e.getActionCommand())) {
// 確認ダイアログ表示
JOptionPane.showConfirmDialog(this, "Confirm");
} else if ("Input".equals(e.getActionCommand())) {
// 入力ダイアログ表示
JOptionPane.showInputDialog("Input");
}
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/08/27 新規作成
TOP →
Java →
Swing →
JOptionPane → This Page