JDialog停止执行父JFrame

时间:2012-12-28 03:05:54

标签: java multithreading swing jdialog animated-gif

我有一个gif动画图像,它在jDialog中显示无限循环加载进度...但问题是当我加载这个jDialog时父框架代码停止。怎么做...这是我的代码...

ProgressDialouge pbDialog = new ProgressDialouge(this);
pbDialog.setVisible(true);
pbDialog.toFront();
postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc"));
postPairs.add(new BasicNameValuePair("user_email",email));
postPairs.add(new BasicNameValuePair("user_password",password));
JSONArray jArray = asyncService.sendRequest(postPairs);
 if(jArray != null){
            new NewJFrame().setVisible(true);

            this.setVisible(false);
  }

如果我改变我的JDiaog的ModalityType.MODELESS,它不会停止执行代码,但它也没有显示进度条..

2 个答案:

答案 0 :(得分:5)

在极有可能的情况下,您遇到了一个线程问题,即您在Swing事件线程上运行长时间运行的任务,从而阻止事件线程更新GUI。解决方案是使用后台线程,例如SwingWorker提供的后台线程。

我的猜测是违规行是这一行:

JSONArray jArray = asyncService.sendRequest(postPairs);

再次,在后台线程中执行此操作。有关详情,请查看以下链接:Concurrency in Swing

例如:

import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class ShowSwingWorker {
   private JPanel mainPanel = new JPanel();
   private JButton myBtn = null;
   private ProgressDialouge pbDialog = null;

   public ShowSwingWorker() {
      myBtn = new JButton(new AbstractAction("Push Me") {

         @Override
         public void actionPerformed(ActionEvent evt) {
            JButton source = (JButton) evt.getSource();
            source.setEnabled(false); // disable button
            Window win = SwingUtilities.getWindowAncestor(source);
            new MySwingWorker().execute(); // start background thread

            if (pbDialog == null) {
               pbDialog = new ProgressDialouge(win);               
               pbDialog.pack();
               pbDialog.setLocationRelativeTo(win);
               Point loc = pbDialog.getLocation();
               pbDialog.setLocation(loc.x - 100, loc.y - 100);
            }
            pbDialog.setVisible(true);
            // pbDialog.toFront();
         }
      });

      mainPanel.add(myBtn);
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   private class MySwingWorker extends SwingWorker<Void, Void> {
      @Override
      protected Void doInBackground() throws Exception {
         Thread.sleep(4000); // emulate a long-running task

         // postPairs.add(new BasicNameValuePair("PATH",
         // "authenticateUser.idoc"));
         // postPairs.add(new BasicNameValuePair("user_email", email));
         // postPairs.add(new BasicNameValuePair("user_password", password));
         // JSONArray jArray = asyncService.sendRequest(postPairs);
         // if (jArray != null) {
         // new NewJFrame().setVisible(true);
         //
         // this.setVisible(false);
         // }
         return null;
      }

      @Override
      protected void done() {
         // Here you change your display.
         // you were swapping JFrames, but I recommend that you instead change views.
         myBtn.setEnabled(true);
         pbDialog.setVisible(false);
      }
   }

   private class ProgressDialouge extends JDialog {

      public ProgressDialouge(Window win) {
         super(win, "MyDialog", ModalityType.APPLICATION_MODAL);
         JProgressBar pBar = new JProgressBar();
         pBar.setIndeterminate(true);
         add(pBar);
      }

   }

   private static void createAndShowGUI() {
      ShowSwingWorker paintEg = new ShowSwingWorker();

      JFrame frame = new JFrame("ShowSwingWorker");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg.getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

答案 1 :(得分:-2)

pbDialog.setVisible(true)行将阻塞,直到在模式对话框中关闭对话框。如果你想让对话框打开而不阻塞,你必须使它成为非模态的。无论动画停止工作的原因是编写同步代码以重新绘制对话框。您需要利用EventQueue定期重绘帧。您可以使用单独的线程,或者您可以使用一些不需要这样的线程的简单代码:

public void paintComponent(Graphics g) {
    if( animate ) {
       Graphics2D g2d = (Graphics2D)g;
       BufferedImage frame = frames[currentFrame];
       g2d.drawImage(frame, null, x, y);
       frame.draw( g );
       currentFrame = (currentFrame + 1) % frames.length;
       repaint(); // this call will schedule a repaint at some point later.
    }
}

这不需要任何线程,这在资源方面是一件好事,并且不太可能搞砸并违反swing的单线程规则。您还可以查看:

http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver)

用于执行GIF动画等动画。