for循环JFrame图像替换

时间:2015-04-10 14:38:39

标签: java multithreading swing for-loop jframe

public void run() {

            frame = new JFrame("JFrame 1");
            Container contentPane = frame.getContentPane();

            JLabel label = new JLabel("labelling.....");
            frame.setPreferredSize(new Dimension(400,600));
            frame.pack();
            frame.setVisible(true);

            // TODO Auto-generated method stub
            try
            {
                for(int i = 0; i < 4; i++)
                {
                    frame.add(new JLabel(new ImageIcon("image1.jpg")));
                    Thread.sleep(000);

                    frame.add(new JLabel(new ImageIcon("image1.jpg")));
                    Thread.sleep(2000);

                    frame.add(new JLabel(new ImageIcon("image1.jpg")));
                    Thread.sleep(2000);
                }
            }
            catch(InterruptedException e)
            {

            }
}

但是它没有更新JFrame。我把Jframe设置在这个类之外...

这个愚蠢的事情要求我提供更多细节,所以这只是华夫饼................................. ...............

1 个答案:

答案 0 :(得分:2)

你不应该像使用Swing GUI一样使用Thread.sleep,因为你将GUI置于睡眠状态。使用Swing Timer可以帮助您交换图像,而无需占用Swing事件线程。此外,不要继续添加JLabel,而是交换单个稳定JLabel的ImageIcon。

这样的东西
  int timerDelay = 500;
  new Timer(timerDelay, new ActionListener() {
     private boolean firstIcon = true;
     private int count = 0;

     @Override
     public void actionPerformed(ActionEvent e) {
        if (count >= MAX_COUNT) {
           ((Timer) e.getSource()).stop(); // stop the timer
           return;
        }

        // swap icons
        Icon icon = firstIcon ? icon1 : icon2;
        label.setIcon(icon);
        firstIcon = !firstIcon;
        count++;
     }
  }).start();

如,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;

public class SwapImages extends JPanel {
   public static final String PATH1 = "https://duke.kenai.com/iconSized/duke.gif"; 
   public static final String PATH2 = "https://duke.kenai.com/iconSized/penduke-transparent.gif";
   public static final String[] PATHS = {PATH1, PATH2};
   protected static final int MAX_COUNT = 8;
   private JLabel label = new JLabel();
   private List<Icon> icons = new ArrayList<>();
   private int index = 0;

   public SwapImages() throws IOException {
      for (String path : PATHS) {
         URL url = new URL(path);
         BufferedImage img = ImageIO.read(url);
         ImageIcon icon = new ImageIcon(img);
         icons.add(icon);
      }
      label.setIcon(icons.get(index));
      add(label);
      int timerDelay = 500;
      new Timer(timerDelay, new ActionListener() {
         private int count = 0;

         @Override
         public void actionPerformed(ActionEvent e) {
            if (count >= MAX_COUNT) {
               ((Timer) e.getSource()).stop();
               return;
            }
            index++;
            index %= icons.size();
            Icon icon = icons.get(index);
            label.setIcon(icon);
            count++;
         }
      }).start();
   }

   private static void createAndShowGUI() {
      SwapImages paintEg = null;
      try {
         paintEg = new SwapImages();
      } catch (IOException e) {
         e.printStackTrace();
      }

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

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