且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何向JTextArea添加文本? (控制台模拟)

更新时间:2023-12-06 16:54:10

一种可能性。您可以使用 JTextArea.append(String)方法:

  JTextArea textArea = new JTextArea 
textArea.append(Your new String);

来自JavaDoc:


将给定的文本附加到文档的末尾。


请注意,您必须自己添加换行符,例如

  textArea.append(Your new String\\\
);



$ p $ p>

b $ b




如果您想要一个真正的自制控制台,请查看 ***a JLine


How does one add text/commands (like in console) into JTextArea?

Or, more specifically, how can I add a JTextArea as console in my FTP program and add commands like C:\tmp\ -list with non-editable text added earlier?

Of course there is a possibility. You can use the JTextArea.append(String) method for this:

JTextArea textArea = new JTextArea();
textArea.append("Your new String");

From JavaDoc:

Appends the given text to the end of the document.

Take care that you have to add line breaks by yourself like

textArea.append("Your new String\n");

if you want to add a new line to the end.


If you want to have a real selfmade console, look for example at ***a or JLine.