Java 如何清理该区域?

Java 如何清理该区域?,java,swing,jtextarea,settext,Java,Swing,Jtextarea,Settext,我正试图清理这个区域 目前,我正在使用 jtextarea.setText(null); 如果我使用 jtextarea.setText(""); 没有区别。它们都具有删除旧文本的效果。从java页面: setText public void setText(String t) Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effe

我正试图清理这个区域

目前,我正在使用

jtextarea.setText(null);
如果我使用

jtextarea.setText("");

没有区别。它们都具有删除旧文本的效果。从java页面:

setText

  public void setText(String t)

  Sets the text of this TextComponent to the specified text. If the text is null
  or empty, has the effect of simply deleting the old text. When text has been
  inserted, the resulting caret location is determined by the implementation of
  the caret class.

  Note that text is not a bound property, so no PropertyChangeEvent is fired when
  it changes. To listen for changes to the text, use DocumentListener.

  Parameters:
      t - the new text to be set
  See Also:
      getText(int, int), DefaultCaret

事实上是有区别的,我想是的


如果将其设置为null,则写入文本区域的实际值将为零。但如果您将其设置为“”,它将是一个空字符。与您可以将其设置为“z”一样,将写入z,但null表示未知。在需要使用textArea中编写的文本之前,您不会显示差异

作者试图清除JTextArea,而不是向其添加空字符

JTextArea0.selectAll();
JTextArea0.replaceSelection("");
    JTextArea0.selectAll();
    JTextArea0.replaceSelection("");
这将选择整个textArea,然后将其替换为空字符串,从而有效地清除JTextArea


不确定这里的误解是什么,但我有相同的问题,这个答案为我解决了。

jtextarea.setText(“”
)在第一次创建它时可能会慢一点(我的意思是,空字符串),因为它缓存在内部哈希集中。但这在当今的系统上甚至都不相关。空字符串在某种程度上可能更具可读性。有了这些问题,您可以查找setText()的源代码并自己查看差异。请修复此链接并参考正确的Swing组件
JTextComponent
,而不是AWT组件。还考虑引用最新的JavaDoc,即,1.7谢谢,现在我可以确定。什么是“空字符”?空字符是一种类似的空间。在我看来。这是一个字符,但如果你把它打印出来。它什么也看不出来。但如果您打印出“null”,它将显示实际文本-null。“”不是空字符,而是零长度字符串。请在您的答案中添加一些文本,以解释代码的作用以及它如何回答初始问题。