如何在 Java 中向 JTextArea 追加文本?
要向 JTextArea 追加文本,请使用组件的 append() 方法。假设 JTextArea 中有以下文本 -
JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "
This demonstrates the usage of JTextArea in Java. ");
现在,向同一 textArea 追加文本 -
textArea.append("In this example we have deleted some text from the beginning."+ "
We have also appended some text.");
以下是如何向 JTextArea 追加文本的示例 -
示例
package my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo { SwingDemo() { JFrame frame = new JFrame("Demo"); JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "
This demonstrates the usage of JTextArea in Java. "); int begn = 0; int end = 10; textArea.replaceRange(null, begn, end); textArea.append("In this example we have deleted some text from the beginning."+ "
We have also appended some text."); frame.add(textArea); frame.setSize(550,300); frame.setLayout(new GridLayout(2, 2)); frame.setVisible(true); } public static void main(String args[]) { new SwingDemo (); } }
输出
广告