在 Java 的 JTextArea 中附加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4852839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending text in Java's JTextArea
提问by newbie
I have a problem with my text area.
我的文本区域有问题。
I use jTextArea1.append("cleverly amusing");
to add the text..
我jTextArea1.append("cleverly amusing");
用来添加文本..
FIRST APPEND:
首先追加:
then I use jTextArea1.append("a fight");
to add the next text.
然后我jTextArea1.append("a fight");
用来添加下一个文本。
SECOND APPEND
第二次追加
What I Really want is to replace the "cleverly amusing" to "a fight". But I cannot do it. I tried to use the jTextArea1.removeAll();
but there's no effect. How can I remove the "cleverly amusing" so that I can append the "a fight" to the first line.
我真正想要的是将“巧妙有趣”替换为“打架”。但我做不到。我尝试使用jTextArea1.removeAll();
但没有效果。如何删除“巧妙有趣”,以便我可以将“战斗”附加到第一行。
NOTE: "WORD HINT" is fixed...
注意:“单词提示”已修复...
What can I do?
我能做什么?
回答by Yanick Rochon
If your JTextArea will onlycontain "WORD HINT: ..." then use the setText() method:
如果您的 JTextArea 将只包含“WORD HINT: ...”,则使用setText() 方法:
jTextArea1.setText("WORD HINT:\n" + word);
This will replace all the text with what you want.
这将用您想要的内容替换所有文本。
BTW : removeAll() is part of the Container class, and is not to remove text but child components.
BTW:removeAll()是Container类的一部分,不是删除文本而是删除子组件。
回答by Qwerky
Why not use setText(String text)
instead of append(String text)
?
为什么不使用setText(String text)
代替append(String text)
?
回答by jzd
Instead of removeAll
, just call setText()
with the first line you want and then you can append the additional data if you want.
而不是removeAll
,只需setText()
使用您想要的第一行调用,然后您可以根据需要附加其他数据。