JOptionPane 从 showOptionDialog 获取字符串

JOptionPane getting a String from showOptionDialog

所以这是我正在处理的项目中经过编辑的一小段代码,我需要能够从此 showOptionDialog 中获取字符串值并将其显示在 showMessageDialog 中,但我我这辈子都想不通,不幸的是,这个网站上唯一与我类似的其他问题对我没有任何帮助。

    String[] buttons = { "Female", "Male" };
    int response = JOptionPane.showOptionDialog(null, "are you Male or Female?", "Important Question.",
            JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);

    JOptionPane.showMessageDialog(null, "You selected that you are a, " + response);

因此在 showMessageDialog 中,根据您按下的按钮,它应该读出如下内容:"You selected that you are a, Female" 或男性,如果用户选择了该选项。

也是的,我意识到我在 showMessageDialog 中放置了一个 int 变量,我只是把它作为占位符放在那里。

如有任何帮助,我们将不胜感激。

您需要做的是:

...
JOptionPane.showMessageDialog(null, "You selected that you are a, " + buttons[response]);