简体   繁体   English

清除 JTextArea 中的文本

[英]Clear text from a JTextArea

I'm writing text on a jPanel: when I press a button it shows text about that button, when i press another one shows text about that one and so on...我在 jPanel 上写文本:当我按下一个按钮时,它会显示有关该按钮的文本,当我按下另一个按钮时会显示有关该按钮的文本,依此类推...

The text area is created like this:文本区域是这样创建的:

    JTextArea log = new JTextArea(1,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);

    add(logScrollPane, BorderLayout.CENTER);

When I display some text with:当我显示一些文本时:

log.append("No file path specified");

I'm not able to delete the previous text.我无法删除以前的文本。 For esample if I press twice the same button i get the string对于 esample,如果我按两次相同的按钮,我会得到字符串

"No file path specifiedNo file path specified" “未指定文件路径未指定文件路径”

I'm not able to clear the text area to display only the new string.我无法清除文本区域以仅显示新字符串。 I've tryed with:我试过:

log.removeAll();

before the log.append() But didn't work.在 log.append() 之前但没有用。

Use either log.setText(null) or log.setText("") , same thing使用log.setText(null)log.setText("") ,同样的事情

Rather the appending text, you should try log.setText("No file path specified");而不是附加文本,您应该尝试log.setText("No file path specified"); , which will replace the current contents with the new String (Thanks Dave) ,它将用新的String替换当前内容(感谢戴夫)

You might like to take some time to read through Using text components for more details您可能需要花一些时间阅读Using text components以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM