在Java Swing中编码时延迟

Wae*_*ael 1 java user-interface swing

我正在使用Java Swing编写GUI应用程序.我只有一个问题,就是我编写了一个在单击按钮时执行的大功能.首先,我将标签的值设置为"生成报告...",然后在函数的"返回"部分之前,我设置标签"完成生成报告"的值.实际发生的是标签的第一个设置没有生效.它们在我完成函数调用后生效,因此最终结果只显示"完成生成报告".

这是我正在使用的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
Run Code Online (Sandbox Code Playgroud)

Gui*_*let 7

将调用移动generateReport到a的方法doInBackgroundSwingWorker.使用方法processdone更新UI元素.

还可以查看"Swing中的并发"