| ■swingコンポーネント->JOptionPane | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPaneはダイアログボックスに配置されるコンポーネントです。アイコン、メッセージやひとつ以上の選択可能な値などを表示させることができます。オプションペインは柔軟性が高く、任意のダイアログを作成するのに使われます。 JOptionPaneのインスタンスを生成してオプションペインのダイアログが生成するが、スタティックなメソッドを呼び出してダイアログを生成することもできます。 | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| コンストラクタ(抜粋) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| メソッド(抜粋) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| OptionSample.java | ||||||||||||||||||||||||||||||||||||||||||
| import java.awt.*; | ||||||||||||||||||||||||||||||||||||||||||
| import java.awt.event.*; | ||||||||||||||||||||||||||||||||||||||||||
| import javax.swing.*; | ||||||||||||||||||||||||||||||||||||||||||
| public class OptionSample extends JApplet implements ActionListener { | ||||||||||||||||||||||||||||||||||||||||||
| Container contentPane; | ||||||||||||||||||||||||||||||||||||||||||
| String[] msgStr = {"ERROR_MESSAGE", | ||||||||||||||||||||||||||||||||||||||||||
| "INFORMATION_MESSAGE", | ||||||||||||||||||||||||||||||||||||||||||
| "WARNING_MESSAGE", | ||||||||||||||||||||||||||||||||||||||||||
| "QUESTION_MESSAGE","PLAIN_MESSAGE"}; | ||||||||||||||||||||||||||||||||||||||||||
| int[] teiStr = {JOptionPane.ERROR_MESSAGE, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.INFORMATION_MESSAGE, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.WARNING_MESSAGE, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.QUESTION_MESSAGE, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.PLAIN_MESSAGE}; | ||||||||||||||||||||||||||||||||||||||||||
| JButton[] btn = new JButton[msgStr.length]; | ||||||||||||||||||||||||||||||||||||||||||
| JPanel msgPanel = new JPanel(); | ||||||||||||||||||||||||||||||||||||||||||
| String[] opStr = {"CLOSED_OPTION", | ||||||||||||||||||||||||||||||||||||||||||
| "YES_NO_OPTION", | ||||||||||||||||||||||||||||||||||||||||||
| "YES_NO_CANCEL_OPTION", | ||||||||||||||||||||||||||||||||||||||||||
| "OK_CANCEL_OPTION"}; | ||||||||||||||||||||||||||||||||||||||||||
| int[] opteiStr = {JOptionPane.CLOSED_OPTION, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.YES_NO_OPTION, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.YES_NO_CANCEL_OPTION, | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.OK_CANCEL_OPTION}; | ||||||||||||||||||||||||||||||||||||||||||
| JButton[] opBtn = new JButton[opStr.length]; | ||||||||||||||||||||||||||||||||||||||||||
| JPanel opPanel = new JPanel(); | ||||||||||||||||||||||||||||||||||||||||||
| public void init() { | ||||||||||||||||||||||||||||||||||||||||||
| contentPane = getContentPane(); | ||||||||||||||||||||||||||||||||||||||||||
| contentPane.setLayout(new GridLayout(1,2)); | ||||||||||||||||||||||||||||||||||||||||||
| msgPanel.setLayout(new FlowLayout()); | ||||||||||||||||||||||||||||||||||||||||||
| for(int i=0; i<msgStr.length; i++){ | ||||||||||||||||||||||||||||||||||||||||||
| btn[i] = new JButton(msgStr[i]); | ||||||||||||||||||||||||||||||||||||||||||
| msgPanel.add(btn[i]); | ||||||||||||||||||||||||||||||||||||||||||
| btn[i].addActionListener(this); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| contentPane.add(msgPanel); | ||||||||||||||||||||||||||||||||||||||||||
| opPanel.setLayout(new FlowLayout()); | ||||||||||||||||||||||||||||||||||||||||||
| for(int i=0; i<opStr.length; i++){ | ||||||||||||||||||||||||||||||||||||||||||
| opBtn[i] = new JButton(opStr[i]); | ||||||||||||||||||||||||||||||||||||||||||
| opPanel.add(opBtn[i]); | ||||||||||||||||||||||||||||||||||||||||||
| opBtn[i].addActionListener(this); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| contentPane.add(opPanel); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| public void actionPerformed(ActionEvent e) { | ||||||||||||||||||||||||||||||||||||||||||
| Object obt = e.getSource(); | ||||||||||||||||||||||||||||||||||||||||||
| for(int i=0; i<msgStr.length; i++){ | ||||||||||||||||||||||||||||||||||||||||||
| if(obt == btn[i]){ | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.showMessageDialog(btn[i], | ← @ | |||||||||||||||||||||||||||||||||||||||||
| msgStr[i], | ||||||||||||||||||||||||||||||||||||||||||
| "JOptionPane", | ||||||||||||||||||||||||||||||||||||||||||
| teiStr[i]); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| for(int i=0; i<opStr.length; i++){ | ||||||||||||||||||||||||||||||||||||||||||
| if(obt == opBtn[i]){ | ||||||||||||||||||||||||||||||||||||||||||
| int n = JOptionPane.showConfirmDialog(opBtn[i], | ← A | |||||||||||||||||||||||||||||||||||||||||
| opStr[i], | ||||||||||||||||||||||||||||||||||||||||||
| "JOptionPane", | ||||||||||||||||||||||||||||||||||||||||||
| opteiStr[i], | ||||||||||||||||||||||||||||||||||||||||||
| JOptionPane.INFORMATION_MESSAGE); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| @JOptionPane.showMessageDialog(btn[i],
msgStr[i], "JOptionPane", teiStr[i]); メッセージを表示するオプションペインをスタティックメソッドで呼び出しています。 メッセージタイプの種類を配列msgStrで用意し、btn[i]が押された時にfor文で該当するメッセージで 生成しています。 |
||||||||||||||||||||||||||||||||||||||||||
| A int
n = JOptionPane.showConfirmDialog(opBtn[i],
opStr[i],"JOptionPane", opteiStr[i], JOptionPane.INFORMATION_MESSAGE); 確認を促すオプションペインをスタティックメソッドで呼び出しています。 ボタンを示すオプションを配列opStrで用意し、opBtn[i]が押された時にfor文で該当するオプション を使って生成しています。 |
||||||||||||||||||||||||||||||||||||||||||
| もどる | ||||||||||||||||||||||||||||||||||||||||||