gpt4 book ai didi

java - 进度监视器(不确定)以显示执行发生在后端

转载 作者:行者123 更新时间:2023-11-30 09:23:50 25 4
gpt4 key购买 nike

我在我的 java 程序中使用 Processbuilder 在后端执行一个外部进程。我正在使用 process.waitFor 来获取进程的退出状态。后端进程需要时间,具体取决于所提供的输入。所以我需要为不确定的时间制作一个进度条,直到进程完成。这只是为了通知用户后端正在进行一个进程。

    try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}
perform(this);
}
}

public void perform(JFrame f)
{
String cmd[]={"cmd", "/c","a.exe:};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File(dir_path));
pb.redirectErrorStream(true);
try {
p = pb.start();
} catch (IOException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
int exitVal = p.waitFor();
if(exitVal>0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);
this.button.setEnabled(true);
}
if(p.waitFor()==0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);

JOptionPane.showMessageDialog(f,"Scan completed successfully.");
}
} catch (InterruptedException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
}

Button 是调用调用的我的按钮的名称。但我面临的问题是函数调用 perform 是在没有执行这些语句的情况下进行的。

    try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}

按钮在该函数执行后被禁用。1)我可以知道为什么会这样吗?2) 我可以知道使进度条指示后端进度直到完成为止的解决方案吗?

提前致谢。

最佳答案

我建议使用 SwingWorker。它是为解决诸如此类的问题而设计的。这是我的代码库中的一些代码片段(vui 是一个 JFrame):

vui.clearOutput();
vui.setOutput("Waiting for items to copy...\n"
+ "This could take several minutes. Please standby...");
vui.disableExit();
vui.disableInput();
vui.disableNext();
vui.showProgressBar();

// make thread so you can disable all options when zipping and enable progress bar
SwingWorker transferWorker = new SwingWorker() {
@Override
protected Void doInBackground() throws Exception {
try {
Process p = Runtime.getRuntime().exec("resources/bin/transferCopier.bat");

StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();

p.waitFor();
} catch (IOException ex) {
Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ie) {
Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ie);
}
return null;
}
@Override
public void done() {
vui.hideProgressBar();
vui.clearOutput();
vui.setOutput("Transfer Complete!\n\n"
+ "Push \"Exit\" to quit.\n\n");
vui.enableExit();
mode = VaderMode.END;
}
};
transferWorker.execute();

当执行 transferWorker.execute(); 时,调用 doInBackground()。一旦 doInBackground() 完成计算,就会调用 done()done() 是您对 GUI 进行任何最终更新的地方。

我上面的代码需要注意的关键是,我在执行 SwingWorker 之前启用了进度条,然后当 SwingWorker 执行完毕后,我禁用了进度条.

资源:

http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

关于java - 进度监视器(不确定)以显示执行发生在后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901648/

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