gpt4 book ai didi

java - 清除 JTextArea 中的文本

转载 作者:行者123 更新时间:2023-12-04 15:46:44 24 4
gpt4 key购买 nike

我在 jPanel 上写文本:当我按下一个按钮时,它会显示有关该按钮的文本,当我按下另一个按钮时,会显示有关该按钮的文本,依此类推...

文本区域是这样创建的:

    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);

当我显示一些文本时:

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

我无法删除之前的文本。对于 esample,如果我按两次相同的按钮,我会得到字符串

"No file path specifiedNo file path specified"

我无法清除文本区域以仅显示新字符串。我试过:

log.removeAll();

在 log.append() 之前但是没用。

最佳答案

使用 log.setText(null)log.setText(""),同样的事情

而不是附加文本,您应该尝试 log.setText("No file path specified");,它将用新的 String 替换当前内容(谢谢戴夫)

您可能想花一些时间通读 Using text components了解更多详情

关于java - 清除 JTextArea 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com