首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清除JTextArea?

如何清除JTextArea?
EN

Stack Overflow用户
提问于 2013-04-04 05:32:11
回答 4查看 77.5K关注 0票数 28

我在试着清除JTextArea。

目前,我正在使用

代码语言:javascript
复制
jtextarea.setText(null);

如果我使用

代码语言:javascript
复制
jtextarea.setText("");
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-04-04 05:38:08

这是没有区别的。它们都具有删除旧文本的效果。在java TextComponent页面中:

setText

代码语言:javascript
复制
  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
票数 26
EN

Stack Overflow用户

发布于 2018-10-23 14:17:17

作者尝试的是清除JTextArea,而不是在其中添加空字符!

代码语言:javascript
复制
    JTextArea0.selectAll();
    JTextArea0.replaceSelection("");

这将选择整个textArea,然后将其替换为空字符串,从而有效地清除JTextArea。

不知道这里的误解是什么,但我也有同样的问题,这个答案为我解决了这个问题。

票数 3
EN

Stack Overflow用户

发布于 2017-05-29 14:32:21

代码语言:javascript
复制
JTextArea0.selectAll();
JTextArea0.replaceSelection("");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15798532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档