簡體   English   中英

如何清除 JTextArea?

[英]How to clear JTextArea?

我正在嘗試清除 JTextArea。

目前,我正在使用

jtextarea.setText(null);

如果我使用有什么區別

jtextarea.setText("");

沒有區別。 它們都有刪除舊文本的效果。 從 java TextComponent頁面:

設置文本

  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

作者試圖清除 JTextArea,而不是向其中添加空字符!

    JTextArea0.selectAll();
    JTextArea0.replaceSelection("");

這將選擇整個 textArea,然后將其替換為空字符串,從而有效地清除 JTextArea。

不知道這里有什么誤解,但我有同樣的問題,這個答案為我解決了。

JTextArea0.selectAll();
JTextArea0.replaceSelection("");

其實是有區別的,我是這么認為的。

如果設置為空,文本區寫入的實際值將是空值。 但是如果你把它設置為 "" 它將是一個空字符。 同樣的,你可以把它設置為“z”,就會寫成z,但null表示不知道。 除非您需要使用在 textArea 中編寫的文本,否則您不會感受到差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM