Java Swing – JOptionPane showOptionDialogの例
JOptionPane
のshowOptionDialog
メソッドは、showConfirmDialog、showInputDialog、およびshowMessageDialogの大統一です。
showOptionDialog
は、Object[]
内でのユーザーの選択の位置を表すinteger
を返します。
Note
さまざまなshowXxxDialogメソッドについて詳しく知りたい場合は、を参照してください。
1. 異なる配列タイプのshowConfirmDialog
オプションarray
は、選択した任意のタイプに設定できます。
ConfirmDialog1a.java
package com.techfou.optiondialog; import javax.swing.*; public class ConfirmDialog1a { public static void main(String[] args) { String[] options = {"abc", "def", "ghi", "jkl"}; //Integer[] options = {1, 3, 5, 7, 9, 11}; //Double[] options = {3.141, 1.618}; //Character[] options = {'a', 'b', 'c', 'd'}; int x = JOptionPane.showOptionDialog(null, "Returns the position of your choice on the array", "Click a button", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); System.out.println(x); } }
出力:
「abc」をクリックすると:
0
「def」をクリックすると:
1
「ghi」をクリックすると:
2
「jkl」をクリックすると:
3
配列をObject[]
として設定することもできます
ConfirmDialog1b.java
package com.techfou.optiondialog; import javax.swing.JCheckBox; import javax.swing.JOptionPane; public class ConfirmDialog1b { public static void main(String[] args) { JCheckBox check = new JCheckBox("Tick me"); Object[] options = {'e', 2, 3.14, 4, 5, "TURTLES!", check}; int x = JOptionPane.showOptionDialog(null, "So many options using Object[]", "Don't forget to Tick it!", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]); if (check.isSelected() && x != -1) { System.out.println("Your choice was " + options[x]); } else { System.out.println(":( no choice"); } } }
出力:
チェックボックスをオンにして「タートルズ!」をクリックすると:
Your choice was TURTLES!
ウィンドウが閉じられるか、ユーザーがチェックボックスをチェックせずにボタンをクリックした場合:
:( no choice
2. showConfirmDialog
showConfirmDialog
がフレームに配置され、アイコンがローカル画像ファイルに設定されている例。
ConfirmDialog2.java
package com.example.optiondialog; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; @SuppressWarnings("serial") public class ConfirmDialog2 extends JFrame { public ConfirmDialog2() { getContentPane().setBackground(new Color(238, 232, 170)); getContentPane().setLayout(null); setTitle("Confirm Dialog in Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); setSize(450, 300); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); setLocation(dim.width / 2 - this.getWidth() / 2, dim.height / 2 - this.getHeight() / 2); } public JPanel panel() { JPanel panel = new JPanel(); panel.setBounds(0, 0, 444, 271); panel.setBackground(new Color(176, 224, 230)); getContentPane().add(panel); panel.setLayout(null); JLabel lblIcon = new JLabel(""); lblIcon.setBounds(30, 30, 200, 200); lblIcon.setIcon(new ImageIcon("src/images/girl200.png")); panel.add(lblIcon); JLabel lblText2 = new JLabel("Lauren is the 4th daughter!"); lblText2.setVerticalAlignment(SwingConstants.TOP); lblText2.setFont(new Font("Tahoma", Font.ITALIC, 14)); lblText2.setHorizontalAlignment(SwingConstants.CENTER); lblText2.setBounds(240, 130, 175, 100); panel.add(lblText2); JLabel lblText1 = new JLabel("Yaaaay!"); lblText1.setHorizontalAlignment(SwingConstants.CENTER); lblText1.setFont(new Font("Tahoma", Font.ITALIC, 14)); lblText1.setBounds(240, 30, 175, 100); panel.add(lblText1); return panel; } public static void main(String[] args) { ConfirmDialog2 cdframe = new ConfirmDialog2(); ImageIcon icon = new ImageIcon("src/images/girl64.png"); String[] options = {"Mary", "Nora", "Anna", "Lauren"}; int x = JOptionPane.showOptionDialog(cdframe, "Lauren's mom had four kids: Maria, Martha, Margaret...", "The missing kid", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, options[0]); if (x == 3) { cdframe.getContentPane().add(cdframe.panel()); cdframe.repaint(); cdframe.revalidate(); } else { cdframe.dispose(); JOptionPane.showMessageDialog(null, "Nooope!"); } } }
出力:
Mary、Nora、Anna、またはX(ウィンドウを閉じるため)をクリックすると:
ローレンがクリックされたとき: