我很确定它应该没问题,但我一直收到这个警告!
String[] trollDo= {"Try and sneak the horses away.",
"Go back and tell the others.",
"Kill the trolls."};
String trollChoice =(String) JOptionPane.showInputDialog(null,
"What will "+playerName+" do?",null,JOptionPane.QUESTION_MESSAGE,
trollDo,trollDo[0]);
答案 0 :(得分:3)
您应该收到编译错误,例如......
error: no suitable method found for showInputDialog(<null>,String,<null>,int,String[],String)
String trollChoice = (String) JOptionPane.showInputDialog(
^
method JOptionPane.showInputDialog(Object) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showInputDialog(Object,Object) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showInputDialog(Component,Object) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showInputDialog(Component,Object,Object) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showInputDialog(Component,Object,String,int) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showInputDialog(Component,Object,String,int,Icon,Object[],Object) is not applicable
(actual and formal argument lists differ in length)
1 error
这意味着您缺少icon
参数...
String trollChoice = (String) JOptionPane.showInputDialog(
null,
"What will " + playerName + " do?",
null,
JOptionPane.QUESTION_MESSAGE,
null, // This one here
trollDo,
trollDo[0]);
确保您正在咨询JavaDocs并使用您的IDE来选择和填充方法参数
答案 1 :(得分:0)
你必须提供适当的参数。
public static Object showInputDialog( Component parentComponent,Object message,String title,int messageType,Icon icon,Object [] selectionValues,Object initialSelectionValue )抛出HeadlessException
JOptionPane.showInputDialog(null, e, playerName, messageType, null, trollDo, playerName);
在阻止对话框中提示用户输入,其中可以指定初始选择,可能的选择和所有其他选项。用户可以从selectionValues中进行选择,其中null表示用户可以通过JTextField输入他们想要的任何内容。 initialSelectionValue是提示用户的初始值。 UI决定如何最好地表示selectionValues,但通常会使用JComboBox,JList或JTextField。
参数:
parentComponent - 对话框的父组件
消息 - 要显示的对象
标题 - 要在对话框标题栏中显示的字符串
messageType - 要显示的消息类型:ERROR_MESSAGE,INFORMATION_MESSAGE,WARNING_MESSAGE,QUESTION_MESSAGE或PLAIN_MESSAGE
图标 - 要显示的图标图片
selectionValues - 提供可能选择的对象数组
initialSelectionValue - 用于初始化输入字段的值
返回:
用户输入,或null表示用户取消了输入
抛出:
HeadlessException - 如果GraphicsEnvironment.isHeadless返回true