1

我正在尝试学习 Java,并且已经从 PHP 中走了很长一段路,我在创建代码时尝试应用相同的心态。但正如你们许多人已经知道的那样,它并不那么容易。

话虽如此,我有一个问题。我想从ArrayList. 我喜欢使用这种JOptionPane.showInputDialog()方法来尝试这个想法。

这是我目前所拥有的,但我收到一条错误消息,告诉我no suitable method found for showInputDialog

ArrayList<String> projectList = new ArrayList<String>();

while(results.next())
    projectList.add(results.getString("project"));

String inputDialog = (String)JOptionPane.showInputDialog(this, "Choose project to open", "Open Project", JOptionPane.PLAIN_MESSAGE, null, projectList, "--");

我知道问题在于,当我将ArrayList数组对象作为数组对象传递时,我会抛出此错误。但是如果我做了类似的事情

Object[] projectList = {"one", "two"};

然后它按预期工作,然后我尝试这样做并projects作为我的数组对象传入。

Object[] projects = {projectList.toString()};

这有点工作,但随后输出看起来像"one, two"下拉列表中的 1 行项目。

4

3 回答 3

2

如果需要,您可以使用List.toArray()ArrayList转换为,即: Object[]

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
Object[] result = list.toArray();
于 2013-04-17T04:19:12.570 回答
1

projectList 是一个 ArrayList,将其转换为数组,然后将其传递给方法 JOptionPane.showInputDialog()。请检查 List.toArray() 方法。

String [] projects = projectList.toArray(new String[projectList.size()]);
于 2013-04-17T04:17:41.827 回答
1

尝试关注

    Object[] projectListArray =new Object[projectList.size()];
    projectList.toArray(projectListArray);

String inputDialog = (String)JOptionPane.showInputDialog(this, "Choose project to open", "Open Project", JOptionPane.PLAIN_MESSAGE, null, projectListArray , "--");

希望能帮助到你。

于 2013-04-17T04:20:12.697 回答