【问题标题】:As far as I know showinputdialog returns an output of type string but it gives me an error that it cannot convert the object to a String据我所知,showinputdialog 返回字符串类型的输出,但它给了我一个错误,它无法将对象转换为字符串
【发布时间】:2019-11-13 09:55:41
【问题描述】:
 String val = JOptionPane.showInputDialog(null,
            "Search item to Edit ",
            "Warning",
            JOptionPane.QUESTION_MESSAGE,
            null,
            new String[]{"Item name", "Item no.","Price"},
            "");

错误:

不兼容的类型:对象无法转换为字符串

【问题讨论】:

  • 附加:如何解决此错误??任何建议都会有很大帮助。谢谢。
  • 您可以在结果上调用toString(),但首先要检查它不是null
  • 您是否尝试过像摇摆教程中建议的那样将结果转换为字符串:docs.oracle.com/javase/tutorial/uiswing/components/…
  • 改进的甲酸盐

标签: java


【解决方案1】:

有了这么多参数,showInputDialog 方法返回的不是字符串,而是 javadoc 中所述的对象

按照Swing tutorial 中的建议,您只需将结果转换为字符串即可。

String val = (String) JOptionPane.showInputDialog(null,
        "Search item to Edit ",
        "Warning",
        JOptionPane.QUESTION_MESSAGE,
        null,
        new String[]{"Item name", "Item no.","Price"},
        "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2017-02-23
    • 2020-04-01
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多