我正在尝试使用 JOptionPane 的静态方法创建一个输入对话框:
public static Object showInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
throws HeadlessException
我的代码如下:
String username = JOptionPane.showInputDialog(null,
"Username",
"Pick a name",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"default_name");
这给我带来了错误:
ChatController.java:49: incompatible types
found : java.lang.Object
required: java.lang.String
一定有一些简单的东西我错过了......
最佳答案
JOptionPane.showInputDialog()
返回一个对象 as specified in the doc ,但你期待的是一个字符串。注意选择选项
Object[] selectionValues
是一个对象
数组,因此您将获得这些对象之一。没有什么可说的,它们被指定为字符串。如果值是字符串,那么您可以/应该进行适当的转换。
另请注意,您正在传递一个空数组。来自文档:
The user will able to choose from selectionValues, where null implies the user can input whatever they wish
关于java - 创建对话框时出现不兼容类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12951124/