java - for循环JFrame图像替换

标签 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...

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

最佳答案

您不应该将 Thread.sleep 与 Swing GUI 一起使用,因为您将使 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();
         }
      });
   }
}

关于java - for循环JFrame图像替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564227/

相关文章:

java - 代号一 - ScaleImageLabel 中不需要的填充/间距

java - 文件的 Junit 测试用例

java - JTextFields 在 JPanel 上的 Activity 绘图之上,线程问题

c - sigwait() 和信号处理程序

java - 删除表中的行无法正常工作

java - 如何通过双击正确删除 JTable 中的行?

java:如何用apache或lighttpd编写服务器?

java - 使用 AES 加密时出现错误填充异常

java - 使用 SwingWorker 高效发布

java - 如果不建议使用挥杆组件后退技巧?