简体   繁体   English

在Java的JTextArea中附加文本

[英]Appending text in Java's JTextArea

I have a problem with my text area. 我的文字区域有问题。

I use jTextArea1.append("cleverly amusing"); 我使用jTextArea1.append("cleverly amusing"); to add the text.. 添加文字..

FIRST APPEND: 第一个附件:

then I use jTextArea1.append("a fight"); 然后我用jTextArea1.append("a fight"); to add the next text. 添加下一个文本。

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(); 我试图使用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. 如何删除“巧妙有趣”,以便我可以将“战斗”添加到第一行。

NOTE: "WORD HINT" is fixed... 注意:“WORD HINT”是固定的......

What can I do? 我能做什么?

If your JTextArea will only contain "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类的一部分,不是删除文本而是删除子组件。

为什么不使用setText(String text)而不是append(String text)

而不是removeAll ,只需使用您想要的第一行调用setText() ,然后您可以根据需要附加其他数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM