這是一個相當奇怪的。我正在使用Swing按鈕來啓動對文件列表的掃描。因爲我希望它在狀態欄上顯示更新,所以我正在使用一個線程。由於Swing在按鈕代碼完成之前不會讓任何東西繪製,因此我還使用Tread將「Start Scan」按鈕更改爲「Stop Scan」按鈕。線程寫入狀態更新文本在鼠標下的任何組件
問題是,如果等待光標放在其他任何組件上,在掃描過程中,狀態消息也正在寫入這些組件,如按鈕(請參閱下面的代碼示例按鈕),複選框等;這弄亂了界面。這是一個重大的錯誤還是不是一個好主意,做我正在做的事情?有沒有辦法解決它?
private void jButton47ActionPerformed(java.awt.event.ActionEvent evt)
{
// Scan folders button.
//
this.getFrame().setCursor(new Cursor(Cursor.WAIT_CURSOR));
// If button is in stop mode then...
if (collection.isScanContinue())
{
collection.setScanContinue(false);
jButton47.setText(" Scan Folders For Files ");
jButton47.setBackground(view.getDefaultButtonCol());
}
else // in scan mode...
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
// Setup the stop scan process button (changes the scan button to a stop button).
//
collection.setScanContinue(true);
jButton47.setText(" Stop Scanning Folders ");
jButton47.setBackground(collection.getPrefs().getDeleteCol());
collection.scanSourceAndTargetFolders();
if(collection.isScanContinue())
{
// do scan
}
// Reset the stop scan button and flag.
//
collection.setScanContinue(false);
jButton47.setText(" Scan Folders For Files ");
jButton47.setToolTipText("Scans Source and, if required, Target folders.");
jButton47.setBackground(view.getDefaultButtonCol());
view.getFrame().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
t.start();
}
}
它清理很好,如果我重新驗證主框架,但它在文件掃描過程中看起來很可怕。
感謝丹尼斯滿口..我會考慮這樣做。 – Robbie62