TOP →
Java →
Swing →
JOptionPane → This Page
JOptionPane@Swing サンプル02
概要
Java -
Swing -
JOptionPane のサンプルです。
・色々なメッセージタイプ
解説
色々なメッセージタイプでメッセージダイアログを表示しています。
サンプルイメージ
メイン画面
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE
サンプルソース
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 サンプル02
* ・色々なメッセージタイプ
*
* @author みっちー
*/
public class JOptionPane02 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JOptionPane02 frame = new JOptionPane02();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JOptionPane サンプル02");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 400, 120);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JOptionPane02() {
// パネルを作成
JPanel panelBase = new JPanel();
// ボタンを作成
JButton button1 = new JButton("ERROR");
JButton button2 = new JButton("INFORMATION");
JButton button3 = new JButton("WARNING");
JButton button4 = new JButton("QUESTION");
JButton button5 = new JButton("PLAIN");
// アクションコマンドを設定
button1.setActionCommand("ERROR");
button2.setActionCommand("INFORMATION");
button3.setActionCommand("WARNING");
button4.setActionCommand("QUESTION");
button5.setActionCommand("PLAIN");
// アクションリスナー追加
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
// ボタンを追加
panelBase.add(button1);
panelBase.add(button2);
panelBase.add(button3);
panelBase.add(button4);
panelBase.add(button5);
// パネルを追加
getContentPane().add(panelBase);
}
/**
* アクション
*/
public void actionPerformed(ActionEvent e) {
if ("ERROR".equals(e.getActionCommand())) {
// エラー
JOptionPane.showMessageDialog(this, "Message1", "title1", JOptionPane.ERROR_MESSAGE);
} else if ("INFORMATION".equals(e.getActionCommand())) {
// 情報
JOptionPane.showMessageDialog(this, "Message2", "title2", JOptionPane.INFORMATION_MESSAGE);
} else if ("WARNING".equals(e.getActionCommand())) {
// 警告
JOptionPane.showMessageDialog(this, "Message3", "title3", JOptionPane.WARNING_MESSAGE);
} else if ("QUESTION".equals(e.getActionCommand())) {
// 質問
JOptionPane.showMessageDialog(this, "Message4", "title4", JOptionPane.QUESTION_MESSAGE);
} else if ("PLAIN".equals(e.getActionCommand())) {
// なし
JOptionPane.showMessageDialog(this, "Message5", "title5", JOptionPane.PLAIN_MESSAGE);
}
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/08/27 新規作成
TOP →
Java →
Swing →
JOptionPane → This Page