简体   繁体   English

在 JtextArea 的末尾添加一个新行

[英]Add a new line to the end of a JtextArea

I have a text area with some text in it and I want to add some lines to it again, (the first lines + the other lines that I want to add) but it doesn't work.我有一个文本区域,其中有一些文本,我想再次向其中添加一些行(第一行 + 我要添加的其他行),但它不起作用。

The way I'm doing it right now erases the old text and shows just the new lines.我现在这样做的方式是删除旧文本并只显示新行。

Instead of using JTextArea.setText(String text) , use JTextArea.append(String text) .不要使用JTextArea.setText(String text) ,而是使用JTextArea.append(String text)

Appends the given text to the end of the document.将给定的文本附加到文档的末尾。 Does nothing if the model is null or the string is null or empty.如果模型为空或字符串为空或空,则不执行任何操作。

This will add text on to the end of your JTextArea .这会将文本添加到JTextArea的末尾。

Another option would be to use getText() to get the text from the JTextArea , then manipulate the String (add or remove or change the String), then use setText(String text) to set the text of the JTextArea to be the new String.另一种选择是使用getText()JTextArea获取文本,然后操作字符串(添加或删除或更改字符串),然后使用setText(String text)JTextArea的文本设置为新字符串.

Are you using JTextArea 's append(String) method to add additional text?您是否使用JTextAreaappend(String)方法添加附加文本?

JTextArea txtArea = new JTextArea("Hello, World\n", 20, 20);
txtArea.append("Goodbye Cruel World\n");

When you want to create a new line or wrap in your TextArea you have to add \\n (newline) after the text.当您想在 TextArea 中创建新行或换行时,您必须在文本后添加 \\n(换行符)。

TextArea t = new TextArea();
t.setText("insert text when you want a new line add \nThen more text....);
setBounds();
setFont();
add(t);

This is the only way I was able to do it, maybe there is a simpler way but I havent discovered that yet.这是我能够做到的唯一方法,也许有更简单的方法,但我还没有发现。

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

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