gpt4 book ai didi

java - 从 JOptionPane 下拉列表框中选择后无法刷新 jtextarea

转载 作者:行者123 更新时间:2023-12-02 07:50:57 25 4
gpt4 key购买 nike

第一次从列表中选择一个选项时,该值显示在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com