繁体   English   中英

似乎无法让JOptionPane下拉菜单正常工作

[英]Can't seem to get the JOptionPane drop-down to work properly

我敢肯定这应该没问题,但我一直收到此警告!

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]);

您应该遇到编译错误,类似...

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来选择和填充方法参数

您必须提供适当的参数。

公共静态对象showInputDialog( 组件parentComponent,对象消息,字符串标题,int messageType,图标图标,Object [] selectionValues,Object initialSelectionValue )引发HeadlessException

 JOptionPane.showInputDialog(null, e, playerName, messageType, null, trollDo, playerName);

在阻止对话框中提示用户输入,在该对话框中可以指定初始选择,可能的选择以及所有其他选项。 用户将可以从selectionValues中进行选择,其中null表示用户通常可以通过JTextField来输入所需的任何内容。 initialSelectionValue是提示用户使用的初始值。 由UI决定如何最好地表示selectionValues,但是通常将使用JComboBox,JList或JTextField。

参数:

parentComponent-对话框的父组件

消息-以显示对象

title-在对话框标题栏中显示的字符串

messageType-要显示的消息类型:ERROR_MESSAGE,INFORMATION_MESSAGE,WARNING_MESSAGE,QUESTION_MESSAGE或PLAIN_MESSAGE

图标-图标图像显示

selectionValues-一个对象数组,给出可能的选择

initialSelectionValue-用于初始化输入字段的值

返回值:

用户输入,或为null表示用户取消了输入

抛出:

HeadlessException-如果GraphicsEnvironment.isHeadless返回true

暂无
暂无

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

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