【发布时间】:2014-10-08 08:01:32
【问题描述】:
我目前正在尝试在我的 java gui 中插入一个进度条。但是,当我单击“开始”按钮时,进度条不会运行。在 cmd 中运行的所有不同批处理文件都必须完成,然后进度条才会运行。但有什么意义呢?我希望它在按下“开始”按钮时运行,并且当 cmd 中运行的所有不同批处理文件结束时,进度条也停止。
这是我的代码:
JLabel ProgressBar = new JLabel("Progress Bar: ");
ProgressBar.setFont(new Font("Arial", Font.BOLD, 12));
ProgressBar.setBounds(100, 285, 180, 53);
contentPane.add(ProgressBar);
final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);
aJProgressBar.setBounds(185,305,184,15);
contentPane.add(aJProgressBar);
//when start button is selected
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent args)
{
//the process starts
JStartTimeTextField.setText(dateFormat.format(date));
//the progress bar should show it running (by right)
aJProgressBar.setIndeterminate(true);
try
{
//create new process
String command = "cmd /c start /wait" +DetectDrive+"\\starting.bat";
//run process
Process p = Runtime.getRuntime().exec(command);
//cause this process to stop until process p is terminated
p.waitFor();
}
catch (IOException | InterruptedException e1)
{
e1.printStackTrace();
}
......
Date newDate = new Date();
JStartTimeTextField.setText(dateFormat.format(newDate));
//the progress bar should stop running (by right)
//aJProgressBar.setIndeterminate(false);
}
});
所以正确...当单击“开始”按钮时,应该显示 startTime 并且应该运行进度条。但是,只有当 cmd 中的所有批处理文件完成运行时,才会显示 startTime。
但现在,进度条是我的首要任务。我怎样才能使它在单击“开始”按钮时,进度条运行,当全部完成时,进度条停止?
我们将不胜感激任何帮助!
【问题讨论】:
-
@MadProgrammer 你知道怎么做吗?
-
@Error 你知道怎么做吗?拼命找答案直接解决
标签: java windows swing user-interface cmd