我正在使用Java Swing編寫GUI應用程序。我只有1個問題,那就是我編寫了一個大功能,當點擊一個按鈕時它會被執行。首先,我將標籤的值設置爲「生成報告」,然後在函數的「返回」部分之前設置標籤「完成生成報告」的值。實際發生的事情是標籤的第一個設置不生效。它們都在我完成函數調用後生效,因此最終結果只顯示「完成生成報告」。使用Java Swing進行編碼時的延遲
這裏是我使用的Java代碼:
protected void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
try
{
jLabel6.setText("Generating Report");
jProgressBar1.setVisible(true);
generateReport(BillImportId.getText()); // This will take around 30 seconds to finish
}
catch (Exception e)
{
System.out.println("Exception occured: " + e);
}
}//GEN-LAST:event_jButton2ActionPerformed
這就是它是如何工作的?你在你的UI線程做你的業務,並阻止用戶界面的更新。即使是那些需要重新繪製標籤的widget也是如此。考慮爲您的長時間運行的進程使用[SwingWorker](http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html)。 – ppeterka 2013-03-04 15:54:04
另請參見[Lesson:Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) – 2013-03-04 15:56:32