繁体   English   中英

据我所知,showinputdialog 返回一个字符串类型的 output 但它给了我一个错误,它无法将 object 转换为字符串

[英]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

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

错误:

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

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

正如Swing 教程中所建议的,您只需将结果转换为字符串。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM