[英]Clear text from a JTextArea
我在 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,如果我按兩次相同的按鈕,我會得到字符串
“未指定文件路徑未指定文件路徑”
我無法清除文本區域以僅顯示新字符串。 我試過:
log.removeAll();
在 log.append() 之前但沒有用。
使用log.setText(null)
或log.setText("")
,同樣的事情
而不是附加文本,您應該嘗試log.setText("No file path specified");
,它將用新的String
替換當前內容(感謝戴夫)
您可能需要花一些時間閱讀Using text components以了解更多詳細信息
從 JTextComponent 超類嘗試 setText
setText("")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.