Java 使用 4 个选项制作 JOptionPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:31:15  来源:igfitidea点击:

Making a JOptionPane with 4 options

javaswingjoptionpane

提问by Mike2012

I need to make a custom dialog with 4 options but as far as I can tell you can only have one with three options. Here is how I would make an option pane with 3 options:

我需要制作一个带有 4 个选项的自定义对话框,但据我所知,您只能有一个带有三个选项的对话框。这是我如何制作一个包含 3 个选项的选项窗格:

        Frame refFrame = DialogUtils.getReferenceFrame();

        ///TODO:
        /// - Use DialogUtils
        int option = JOptionPane.showOptionDialog(refFrame,
            msg,
            rsc.str("918"),
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            DialogUtils.INFO_ICON,
            options,
            options[0]);

But I could not find some sort of open ended substitution for YES_NO_CANCEL_OPTION. Is there a way to make the JOptionPane allow four choices?

但是我找不到 YES_NO_CANCEL_OPTION 的某种开放式替代。有没有办法让 JOptionPane 允许四个选择?

采纳答案by Peter

You can use any of the JOptionPane's option constants, you just need to supply a options array of size 4:

你可以使用任何 JOptionPane 的选项常量,你只需要提供一个大小为 4 的选项数组:

public static void main(String[] args) {
    String[] options = new String[] {"Yes", "No", "Maybe", "Cancel"};
    int response = JOptionPane.showOptionDialog(null, "Message", "Title",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
        null, options, options[0]);

    // Where response == 0 for Yes, 1 for No, 2 for Maybe and -1 or 3 for Escape/Cancel.
}

回答by Michael Borgwardt

Simply use an optionsarray of size 4 instead of 3...

只需使用options大小为 4 而不是 3的数组...