0

jDialog内で無限の円の読み込みの進行状況を示すgifアニメーション画像があります...しかし、この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を変更しても、コードの実行は停止しませんが、進行状況バーも表示されません。

4

2 に答える 2

5

おそらく、Swingイベントスレッドで長時間実行されるタスクを実行しているというスレッドの問題があり、イベントスレッドがGUIを更新できません。解決策は、SwingWorkerによって提供されるようなバックグラウンドスレッドを使用することです。

私の推測では、問題のある行は次のとおりです。

JSONArray jArray = asyncService.sendRequest(postPairs);

繰り返しになりますが、これはバックグラウンドスレッドで行います。詳細については、次のリンクを確認してください: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();
         }
      });
   }
}
于 2012-12-28T03:09:42.367 に答える
-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 などでアニメーションを実行する場合。

于 2012-12-28T03:15:40.297 に答える