2

我想创建一个在设备中安装文件的应用程序。但是我在实现进度时遇到了问题,我在框架中使用来调用要安装的类的代码如下所示,execShellCmd 是调用安装到所有设备的方法。value 是 Install 类给出的静态值。我想实现一个用于安装和评估的进度条,以便提供安装进度。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
     Thread t;  
        t = new Thread(){   
        private int postion;
     public void run(){  
        Install install = new Install();
        int position = 0;
        String fileName = directory;
        String shellCommand = fileName;

       // for (int position =0; postion < 105;position +5) {
                jProgressBar1.setValue(Install.value);
                try {
                        Thread.sleep(500);
                } catch (InterruptedException e) {
                }
                position += 5;
        //}
        install.execShellCmd(shellCommand);
        //jTextArea1.setText(error.err.toString());
        }

        };
        t.start();
    }
4

2 回答 2

2

As shown here, you can use ProcessBuilder to execute your script. Parse the combined streams, obtained from getInputStream(), to assess the script's actual progress. Use that information to condition your JProgresBar. To preserve liveness in the GUI, do this in the doInBackground() method of a SwingWorker, from which you can invoke setProgress().

于 2013-03-05T16:29:19.517 回答
1

嗯..据说这种线程方式不适合JProgressBar。相反,移动Timer. 我在线程内部使用 JProgressBar 时遇到了同样的问题。使用以下链接获取更多信息,它也有工作示例

http://www.coderanch.com/t/566728/GUI/java/JProgressBar-Napkin-feel-working

是的,看看我在下面的回答

更新jProgressBar的方法是什么?

于 2013-03-05T18:27:49.580 回答