繁体   English   中英

使用Java的JOptionPane

[英]Using Java's JOptionPane

我正在尝试使用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...

JOptionPane.showInternalInputDialog仅与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

讨论用户参数,其值或其顺序不正确的异常,

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();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM