1

我正在尝试在我的应用程序中添加一个根据命令行或 Java 代码进行的进度条,我的应用程序是另一种程序的设置或安装 Etaps。

我发现的所有进度条都没有与复制任务交互,这意味着我不知道复制任务何时完成。

我希望我的进度条在这些行命令的功能中取得进展:

try {
            Runtime.getRuntime().exec("cmd.exe /k mkdir \""+dossier+"\\results\" &&"
                    + " mkdir \""+dossier+"\\results\\basededonnees\" &&"
                    + " mkdir \""+dossier+"\\results\\ordonnances\" &&"
                    + " mkdir \""+dossier+"\\results\\piecesjoins\"");

            Runtime.getRuntime().exec("cmd.exe /k mkdir \""+dossier+"\\basededonnees\" &&"
                    + " echo CREATE DATABASE "+nomBase+"; >> \""+dossier+"\\basededonnees\\CreerDB.sql\"");

            Runtime.getRuntime().exec("cmd.exe /k xcopy apps\\lib \""+dossier+"\\lib\" &&"
                    + " set path=%path%;C:\\wamp\\bin\\mysql\\mysql5.5.20\\bin &"
                    + " set path=%path%;C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin &"
                    + " mysql -u root < \""+dossier+"\\basededonnees\\CreerDB.sql\"");

    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Une erreur est survenue \n" + ex.getMessage(), "Information", JOptionPane.INFORMATION_MESSAGE);
    }

档案 = 'C:\Program Files\NameOfMyApps' nomBase = 'NameOfMyDataBase'

当所有命令和复制任务完成后,我希望栏达到 100%。

提前谢谢你,对不起语言,因为我没有衡量好英语

为了更好地解释这里是一个截图: 在此处输入链接描述

http://www.4shared.com/download/-b2S2bFN/apps.jpg?tsid=20130605-124016-afcd18b4

4

1 回答 1

1

您好,请看下面的示例

package com.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Copy {

    private static final String PING_LOCALHOST = "ping localhost";

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        ProgressBar progressBar = new ProgressBar();
        progressBar.showProgress();
        // first command
        runCommand(progressBar, PING_LOCALHOST, 25);
        runCommand(progressBar, PING_LOCALHOST, 50);
        runCommand(progressBar, PING_LOCALHOST, 75);
        runCommand(progressBar, PING_LOCALHOST, 100);

    }

    private static void runCommand(ProgressBar progressBar, String command, int barValue) throws InterruptedException {
        CopyThread copyThread = new Copy().new CopyThread(command);
        copyThread.start();
        // wait for thread to die
        copyThread.join();
        progressBar.updatePercent(barValue);
    }

    private class CopyThread extends Thread {
        private String cmd;

        public CopyThread(String command) {
            this.cmd = command;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                String line;
                Process p = Runtime.getRuntime().exec(cmd);
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
                input.close();
            } catch (Exception err) {
                err.printStackTrace();
            }
        }

    }

}

进度条

package com.test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class ProgressBar {

    private JFrame progressFrame;
    private JProgressBar progressBar;

    /**
     * Create the application.
     */
    public ProgressBar() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        progressFrame = new JFrame();
        progressFrame.setTitle("Test progress bar");
        progressFrame.setResizable(false);
        progressFrame.setBounds(100, 100, 640, 79);
        progressFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        progressFrame.getContentPane().setLayout(null);

        progressBar = new JProgressBar();
        progressBar.setFont(new Font("Tahoma", Font.PLAIN, 16));
        progressBar.setBackground(new Color(255, 255, 255));
        progressBar.setIndeterminate(false);
        progressBar.setStringPainted(true);
        progressBar.setForeground(new Color(0, 128, 0));
        progressBar.setBounds(0, 0, 634, 52);

        progressFrame.getContentPane().add(progressBar);
    }

    public void showProgress() {
        initialize();
        progressFrame.setVisible(true);
    }

    public void closeProgress() {
        progressFrame.dispose();
        progressFrame.setVisible(false);
    }

    public void updatePercent(int value) {
        progressBar.setValue(value);
    }
}
于 2013-06-05T12:58:45.897 回答