第一次从列表中选择一个选项时,该值显示在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");
}
发布于 2012-04-19 17:41:44
fruit.getName()
fruit.getColor()
这两个是错误的,因为它是对JOptionPane返回内容的引用,而不是带有getter的对象。因为它不是您的数组,所以choices
包含字符串。只需附加joptionpane返回的fruit
(这将是苹果、橙子或梨):
jtextarea.append("Name\t: " + fruit + "\n");
https://stackoverflow.com/questions/10233529
复制