我第一次从列表中选择一个选项时,该值会显示在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");
}
答案 0 :(得分:2)
fruit.getName()
fruit.getColor()
这两个是错误的,因为它是对你的JOptionPane返回的内容的反对,而不是具有getter的对象。因为它不是你的数组choices
包含字符串。只需附上您的joptionpane返回的fruit
(这将是Apple,Orange或Pear):
jtextarea.append("Name\t: " + fruit + "\n");