将文本附加到TextArea中 [英] Appending text into TextArea
问题描述
我正在尝试创建 TextArea
,
@FXML
private TextArea ta;
以及我想要得到的东西:
and what I'm trying to get :
for (int i=1; i<=5; i++) {
ta.setText(" Field " + i + "\n");
}
但它只显示最后一行:字段5
。
任何人都可以提供帮助。提前致谢。
but it only show the last line : Field 5
.
Can anyone help. Thanks in advance.
推荐答案
当你打电话给 setText(...)
时,你替换已经存在的文本。所以要么在设置之前构造你的String,要么追加它。
试试这个:
When you call setText( "...")
, you replace the text which is already there. So either construct your String before setting it, or append it.
Try this:
String text="";
for (int i=1;i<=5;i++) {
text = text + " Field "+i+"\n";
}
ta.setText(text);
注意:您可能会获得更好的性能,并且使用StringBuilder被认为是好习惯 而不是像这样构建String的String。但这应该可以帮助你理解问题所在,而不会让它变得过于复杂。
Note: You'll probably get better performance and it's considered "good practice" to use a "StringBuilder" instead of a String to build String like this. But this should help you understand what's the problem, without making it overly complicated.
这篇关于将文本附加到TextArea中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!