要将文本追加到JTextArea文档末尾,我们可以使用append(String str)方法。如果文档为null或字符串为null或为空,则此方法不执行任何操作。
package org.nhooo.example.swing; import javax.swing.*; import java.awt.*; public class TextAreaAppendText extends JPanel { public TextAreaAppendText() { initializeUI(); } private void initializeUI() { String text = "The quick brown fox "; JTextArea textArea = new JTextArea(text); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane scrollPane = new JScrollPane(textArea); String appendText = "jumps over the lazy dog."; textArea.append(appendText); this.setPreferredSize(new Dimension(500, 200)); this.setLayout(new BorderLayout()); this.add(scrollPane, BorderLayout.CENTER); } public static void showFrame() { JPanel panel = new TextAreaAppendText(); panel.setOpaque(true); JFrame frame = new JFrame("JTextArea Demo"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { TextAreaAppendText.showFrame(); } }); } }
上面的代码片段的输出是: