Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何让我的进度条在;“开始”;按钮被点击了吗?_Java_Windows_Swing_User Interface_Cmd - Fatal编程技术网

Java 我如何让我的进度条在;“开始”;按钮被点击了吗?

Java 我如何让我的进度条在;“开始”;按钮被点击了吗?,java,windows,swing,user-interface,cmd,Java,Windows,Swing,User Interface,Cmd,我目前正试图在我的JavaGUI中插入一个进度条。但是,当我单击“开始”按钮时,进度条不会运行。cmd中运行的所有不同批处理文件都必须完成,然后进度条才会运行。但这有什么意义呢?我希望它在按下“开始”按钮时运行,当cmd中运行的所有不同批处理文件结束时,进度条也停止 这是我的密码: JLabel ProgressBar = new JLabel("Progress Bar: "); ProgressBar.setFont(new Font("Arial", Font.BOLD, 12)); Pr

我目前正试图在我的JavaGUI中插入一个进度条。但是,当我单击“开始”按钮时,进度条不会运行。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);
    }
});
所以说对了。。。单击“开始”按钮时,应显示开始时间,且进度条应处于运行状态。但是,只有当cmd中的所有批处理文件完成运行时,才会显示startTime

但现在,进度条是我的首要任务。我怎样才能使单击“开始”按钮时,进度条运行,当所有操作完成时,进度条停止


任何帮助都将不胜感激

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

比如:

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.setUndeterminate(false)
未注释,在右边,它应该运行。但事实并非如此。但是如果它被注释,当点击“开始”按钮时,进度条能够运行。但在进程结束时不会停止。是的,此代码不监视
进程
线程的结束。如果要这样做,就必须使用同步机制。但是我真的建议你改用SwingWorker。但是你知道怎么换成SwingWorker吗?因为在我的编码中,我有太多的编码,我不知道如何将其更改为swingworker@MadProgrammer你知道怎么做吗?@Error你知道怎么做吗?拼命寻找一个直接解决问题的答案