从 JOptionPane 输入对话框获取选定值

Getting selected value from a JOptionPane input dialog

我正在尝试弄清楚如何从带有组合框的 JOptionPane 对话框中获取选定的值(好吧,有点)。

代码如下:

String [] locations = { "Brisbane", "Sydney", "Melbourne" };
Object city = (String) JOptionPane.showInputDialog(
    null,
    "Location?",
    "Hills School",
    JOptionPane.PLAIN_MESSAGE,
    null,
    locations,
    locations[0]);

正如您可能看到的那样,我实际上并没有使用组合框,这正是我遇到问题的地方。它仍然在 JOptionPane 中显示为组合框,但我不能使用 getSelectedValue()。我查看了 java 网站,但似乎无法找到有关如何获取所选值的答案。

有没有办法做到这一点,或者我应该从不同的角度进入 JOptionPane?

谢谢

所选值在您的 city 变量中,根据转换它是一个字符串。