【问题标题】:Getting selected value from a JOptionPane input dialog从 JOptionPane 输入对话框中获取选定的值
【发布时间】:2018-03-18 10:03:03
【问题描述】:

我试图弄清楚如何从带有组合框的 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?

谢谢

【问题讨论】:

    标签: java joptionpane


    【解决方案1】:

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

    【讨论】:

    • 那么我可以用什么方法来获取选中的值呢?我发现 getValue() 对对象有效,但仍然返回错误。
    • Object city 更改为String city 或使用city.toString()
    • 太棒了。非常感谢。
    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多