使用Java的JOptionPane

时间:2013-10-10 09:48:31

标签: java swing joptionpane

我正在尝试使用Java JOptionPane模块。这是代码:

Object[] options = {"OK", "Cancel"};
JOptionPane.showInternalOptionDialog(null, "Your choice", "Division", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);

error: JOptionPane: parentComponent does not have a valid parent at...

2 个答案:

答案 0 :(得分:1)

JOptionPane.showInternalInputDialog仅与JDesktopPane / JInternalFrames一起使用,其中这是JDesktopPane / JInternalFrames实例。

final JDesktopPane deskpane = new JDesktopPane();
...
String str=JOptionPane.showInternalInputDialog(deskpane, "Enter value");

如果不与上述两个组件中的任何一个一起使用,它将不会产生正确的输出,实际上它会抛出一个运行时异常:

java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid pa

答案 1 :(得分:1)

关于不正确的用户参数,其价值或订单的讨论,

import java.awt.EventQueue;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class MyOptionPane {

    public MyOptionPane() {
        Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
        Object[] possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
        Integer i = (Integer) JOptionPane.showOptionDialog(null, 
                null,  "ShowInputDialog", 
                JOptionPane.PLAIN_MESSAGE,1,  errorIcon, possibilities, 0);
        Integer ii = (Integer) JOptionPane.showInputDialog(null,
                "Select number:\n\from JComboBox", "ShowInputDialog",
                JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyOptionPane mOP = new MyOptionPane();
            }
        });
    }
}