Making a JOptionPane with 4 options

 ̄綄美尐妖づ 提交于 2019-12-29 07:37:32

问题


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:

        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?


回答1:


You can use any of the JOptionPane's option constants, you just need to supply a options array of size 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.
}



回答2:


Simply use an options array of size 4 instead of 3...



来源:https://stackoverflow.com/questions/1257420/making-a-joptionpane-with-4-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!