第一次从列表中选择一个选项时,该值显示在 jtextarea 上 第二次选择某个选项时,该值不会改变。 有刷新选项吗?或者解决这个问题的更好方法?谢谢!
这是代码片段:
String[] choices = {"Apple","Orange", "Pear"};
String fruit= (String) JOptionPane.showInputDialog(null, "Select Fruit:","Select Fruit", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if (fruit!= null){
jtextarea.append("Name\t: " + fruit.getName() + "\n");
jtextarea.append("Color\t: " + fruit.getColor() + "\n");
}
最佳答案
fruit.getName()
fruit.getColor()
这两个是错误的,因为它是对 JOptionPane 返回的内容的引用,而不是带有 getter 的对象。因为它不是您的数组 choices
包含字符串。只需附加您的 joptionpane 返回的内容,即 fruit
(可以是 Apple、Orange 或 Pear):
jtextarea.append("Name\t: " + fruit + "\n");
关于java - 从 JOptionPane 下拉列表框中选择后无法刷新 jtextarea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10233529/