如何清除JTextArea?

Lor*_*uel 26 java swing clear jtextarea settext

我正在尝试清除JTextArea.

目前,我正在使用

jtextarea.setText(null);
Run Code Online (Sandbox Code Playgroud)

如果我使用有什么区别

jtextarea.setText("");
Run Code Online (Sandbox Code Playgroud)

Kev*_*n S 24

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

的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
Run Code Online (Sandbox Code Playgroud)