1

我有一个将文件(通过 ADB)复制到 android 平板电脑的应用程序。这需要一些时间,所以我想显示一个带有不确定进度条的弹出窗口。复制任务完成后,我希望能够停止进度条并让用户关闭对话框。

目前我还没有添加额外的对话框,只是想让进度条正常工作。我遇到的问题是任务开始时没有显示进度条,但我不知道为什么。当出现同步完成对话框时会显示进度条。代码是:

        progress = new JProgressBar(0, 100);
        progress.setForeground(new Color(255, 99, 71));
        progress.setIndeterminate(true);
        progress.setValue(0);
        progress.setPreferredSize( new Dimension( 300, 20 ) );
        progress.setBounds( 278, 12, 260, 20 );
        progress.setVisible(false);
        progress.setString("Sync in progress");
        progress.setStringPainted(true);
        contentPane.add(progress);
        pushtotab = new JButton("");
        pushtotab.addActionListener(new ActionListener() {


 public void actionPerformed(ActionEvent arg0) {
                        if (buildpathset==1){
                            try{
                            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                            progress.setVisible(true);
                            wiredsync();
                        }finally{
                            JOptionPane.showMessageDialog(null, "sync complete. ",null, buildpathset);
                             setCursor(Cursor.getDefaultCursor());      
                             progress.setVisible(false);
                        }}else{ 
    //warning in here later - TO Do
                }
                }
                });

public void wiredsync(){

        try {

                    Process process = Runtime.getRuntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync);
                    InputStreamReader reader = new InputStreamReader(process.getInputStream());
                    Scanner scanner = new Scanner(reader);
                    scanner.close();
                    int exitCode = process.waitFor();
                    System.out.println("Process returned: " + exitCode);

                } catch(IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }//end 

谢谢您的帮助,

安迪

4

2 回答 2

2

我认为你的问题是你不使用线程。我的意思是在您将进度条的可见性设置为 true 之后,您应该在线程中定义您的长任务。我对 Swing 不熟悉,但请在那里寻找 Swing(对不起,如果它没有用完): http ://www.java-tips.org/java-se-tips/javax.swing/how-to-handle- swing-applic.html 中长时间运行的任务

安卓系统:http ://www.mkyong.com/android/android-progress-bar-example/

于 2013-06-29T16:11:03.007 回答
2

pooyan 有正确的想法——在后台线程中执行长时间运行的进程——但给出了错误的库示例,因为您的程序是 Swing 程序而不是 Android 程序。Swing 对此的典型答案是doInBackground()使用 SwingWorker 的方法来完成您的长时间运行的任务。

请稍等,我找到一个更好的例子......

像这样:

if (buildpathset == 1) {
   setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
   progress.setVisible(true);

   // create my SwingWorker object
   final SwingWorker<Void, Void> myWorker = new SwingWorker<Void, Void>() {
      protected Void doInBackground() throws Exception {
         // here is my long running task, calling in background
         // thread
         wiredsync();
         return null;
      };
   };

   // this allows me to be notified when the SwingWorker has
   // finished
   myWorker.addPropertyChangeListener(new PropertyChangeListener() {

      @Override
      public void propertyChange(PropertyChangeEvent pcEvt) {
         // if the SwingWorker is done
         if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
            // notify the user
            JOptionPane.showMessageDialog(null, "sync complete. ",
                  null, buildpathset);
            setCursor(Cursor.getDefaultCursor());
            progress.setVisible(false);

            try {
               // one way to catch any errors that occur in
               // SwingWorker
               myWorker.get();
            } catch (InterruptedException | ExecutionException e) {
               e.printStackTrace();
            }

         }
      }
   });
   // run my SwingWorker
   myWorker.execute();
} else {
   // warning in here later - TO Do
}

有关这方面的更多信息,请查看:课程:Swing 中的并发性

于 2013-06-29T16:28:21.937 回答