【问题标题】:How do I make my progress bar run when the "START" button is clicked?单击“开始”按钮时,如何使进度条运行?
【发布时间】: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


【解决方案1】:

您正在使用相同的线程来更新JProgressBar 并运行脚本。问题是您等待Process 终止(p.waitFor()),因此,线程将等待而不更新JProgressBar。正确的解决方案是使用SwingWorker 。但我认为你可以通过启动一个新的Thread 来运行你的脚本来运行它。

类似:

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);
    new Thread()
    {
    public void run()
    {
    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();
    }
    }
    }.start();   

    ......

    Date newDate = new Date();
    JStartTimeTextField.setText(dateFormat.format(newDate));

    //the progress bar should stop running (by right)
    //aJProgressBar.setIndeterminate(false);
}

【讨论】:

  • 这会在进程甚至还没有完成时一起显示 startTime 和 endTime。然后aJProgressBar.setIndeterminate(false); 未注释,它应该运行。但事实并非如此。但是如果它被注释,当点击“开始”按钮时,进度条能够运行。但不会在进程结束时停止。
  • 是的,这段代码不监控Process线程的结束。如果你想这样做,你将不得不使用同步机制。但我真的建议你改用 SwingWorker。
  • 但是你知道怎么改成swingWorker吗?因为在我的编码中,我有太多的编码,我不知道如何将其更改为 swingworker
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多