Java - 在何处以及如何向调用批处理作业的现有Swing应用程序添加不确定的进度条

时间:2017-04-29 22:17:01

标签: java swing swingworker

我需要向调用批处理作业的现有Java Swing应用程序添加一个不确定的进度条。单击Start Job按钮后,我需要进度条开始运行,但批处理作业完成后,我需要删除/消失。
最初,我遇到了SwingWorker类的问题,但是在其他用户的示例(请参阅注释)的帮助下,我需要的代码如下:

更新/编辑:感谢评论,下面的代码完成了我的需要,是一个完整的(虽然是微不足道的)应用程序。

注意:代码中的批处理脚本(即C:/MyProgress/Test.bat)只需睡眠 20秒然后退出。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;

public class MyProgress extends JPanel
  implements ActionListener {
  private static final long serialVersionUID = 1L;

  JButton bt12, bt13;
  JProgressBar progressBar = new JProgressBar();

  public MyProgress() {

    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;

    gbc.gridy++;
    gbc.fill = GridBagConstraints.EAST;
    gbc.gridwidth = 1;
    bt12 = new JButton("Start Job");
    bt12.setForeground(Color.GREEN);
    bt12.addActionListener(this);
    add(bt12, gbc);

    gbc.fill = GridBagConstraints.WEST;
    gbc.gridwidth = 2;
    bt13 = new JButton("Exit GUI");
    bt13.setForeground(Color.RED);
    bt13.addActionListener(this);
    add(bt13, gbc);

    gbc.gridy++;
    gbc.fill = GridBagConstraints.CENTER;
    gbc.gridwidth = 10;
    add(progressBar, gbc);

  }   

  public void actionPerformed(ActionEvent e) {

      progressBar.setIndeterminate(true);
      progressBar.setString("Processing ...");
      progressBar.setStringPainted(true);
      progressBar.setForeground(Color.red);

      if (e.getActionCommand().matches("Start Job")) {

        class MyWorker extends SwingWorker<String, Object> {
           protected String doInBackground() {     
             progressBar.setVisible(true); 

             String cmd = "C:/MyProgress/Test.bat";

             Process p = null;
             try {
                p = Runtime.getRuntime().exec(cmd);
             } catch (IOException e) {
                e.printStackTrace();
             }

             try {
               p.waitFor();
             } catch (InterruptedException ite) {
               ite.printStackTrace();
             }

             return "done"; 
             }

           protected void done() {
              progressBar.setVisible(false);
           }  
        } 

        new MyWorker().execute();

      } else if (e.getActionCommand().matches("Exit GUI")) {
             System.exit(0);                                                
      }
  }

  public static void main(String s[]) {

    JFrame frame = new JFrame("MyProgress");
    MyProgress panel = new MyProgress();

    frame.getContentPane().add(panel,"Center");

    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
    });

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(new Dimension(200, 200)); 
    frame.setLocationRelativeTo(null);          
    frame.setVisible(true);    

   } 
}  

0 个答案:

没有答案