gpt4 book ai didi

java - 进度条,在命令行或复制任务功能中取得进展

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:31 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中添加一个根据命令行或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%。

预先感谢您,并对语言表示歉意,因为我英语不好

为了更好地解释这里有一个屏幕截图: enter link description here

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

最佳答案

您好,请参阅下面的示例

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);
}
}

关于java - 进度条,在命令行或复制任务功能中取得进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com