2013-02-28 70 views
0

我有一個帶有打開和後退按鈕的窗體。通過打開按鈕打開我的批處理文件,當批處理文件正在執行時,其他按鈕被禁用。我想啓用這些按鈕。請幫幫我。在執行.bat文件時啓用按鈕

運行批處理文件代碼:

private void openActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    // back.setEnabled(true); 
    String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat"; 
    String command = filename; 
    Runtime runtime = Runtime.getRuntime(); 
    try { 
     Process process = runtime.exec(command); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line = reader.readLine(); 
     while (line != null) { 
      System.out.println(line); 
      line = reader.readLine(); 
      System.out.println(line); 
     } 
     back.setEnabled(true); 
     JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!"); 
    } catch (IOException e) { 

     JOptionPane.showMessageDialog(null, "Batch file execution failed."); 
    } 
    // Form f=new Form(); 
    // back.action(f.setVisible(true),null); 

} 

後退按鈕代碼:

private void backActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    close(); 
    sms_sound s = new sms_sound(); 
    //s.setVisible(true); 
    Form f = new Form(); 
    f.setVisible(true); 
    // back.setEnabled(true); 
} 
+0

我要指出,你的其他問題是回按鈕將只能重新啓用,如果過程沒有失敗......不知道這是否你想要的功能,但它看起來很腥 – MadProgrammer 2013-02-28 06:56:20

回答

2

您阻止事件分派線程,這是防止它處理任何重繪請求。

您應該將「批處理」代碼移動到後臺線程,像SwingWorker這樣的問題對於這個問題非常好。

新增例如

public class BatchRunner extends SwingWorker<Integer, String> { 

    @Override 
    protected Integer doInBackground() throws Exception { 
     String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat"; 
     String command = filename; 
     Runtime runtime = Runtime.getRuntime(); 
     Process process = runtime.exec(command); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
      publish(line); 
     } 
     return process.exitValue(); 
    } 

    @Override 
    protected void process(List<String> chunks) { 
     // You can process the output produced 
     // the doBackgroundMethod here within the context of the EDT 
    } 

    @Override 
    protected void done() { 
     try { 
      Integer result = get(); 
      if (result == 0) { 
       JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!"); 
      } else { 
       JOptionPane.showMessageDialog(null, "Batch file returned an exit value of " + result); 
      } 
     } catch (InterruptedException | ExecutionException | HeadlessException interruptedException) { 
      JOptionPane.showMessageDialog(null, "Batch file execution failed."); 
     } 
     back.setEnabled(true); 
    } 
} 

當你準備執行批處理程序,只需這樣做...

back.setEnabled(false); 
new BatchRunner().execute();