0

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

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...

4

2 回答 2

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();
            }
        });
    }
}
于 2013-10-10T10:17:01.557 回答
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
于 2013-10-10T09:55:10.843 回答