1

我有一个小问题试图弄清楚如何检查用户是否按下了自定义 JOptionPane 中的按钮。

我的对话框基于一个 inputDialog,其中包含 YES、NO 和 CANCEL 按钮(“选择”、“取消”、“打开编辑器”)的自定义文本。

我尝试寻找解决方案,但我发现的只是使用静态 JOptionPane 函数的问题。

这是我现在使用的代码:

public SelectItemDialog(Component parent) {
    super("Please select an item:", YES_NO_CANCEL_OPTION, PLAIN_MESSAGE, Editor.getIcon("bookmark"),
        new String[] { "Select", "Cancel", "Open Item Editor" }, "Select"
    );

    setWantsInput(true);

    setSelectionValues(null); // Would replace with an Object array
    setInitialSelectionValue(null);

    setComponentOrientation(getRootFrame().getComponentOrientation());

    JDialog dialog = createDialog(parent, "Select Item");
    selectInitialValue();

    dialog.setVisible(true);
    dialog.dispose();

    Object obj = getInputValue();

    if(obj instanceof Item) {
        this.openEditor = false;
        this.item = (Item) obj;

    } else {
        this.openEditor = (obj.equals( CANCEL_OPTION));
        this.item = null;
    }
}

对 CANCEL_OPTION 的检查根本不起作用,与 UNDEFINED_OPTION 相同。

有任何想法吗?

4

1 回答 1

0

实际上我只需要使用 JOptionPane 本身返回的 Object: getValue(),问题就解决了!

于 2013-07-24T16:55:47.813 回答