如何自动更改jlable中的图像

时间:2016-05-21 09:22:13

标签: java swing jlabel

我试图在3种不同的jlables中显示不同的图像。有一个包含图像的文件夹。我在jlable中显示图像,图像必须在60秒后改变。无论我尝试什么,我都做不到。有人可以帮助我。

1 个答案:

答案 0 :(得分:2)

简单的答案是,使用Swing Timer,它允许您在将来调度回调,这是在事件调度线程的上下文中提供的,从而可以安全地更新UI,示例...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private File[] images;
        private int imageIndex;

        public TestPane() {
            setLayout(new BorderLayout());
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            add(label);
            images = new File("...").listFiles();
            imageIndex = -1;
            nextImage();
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    nextImage();
                }
            });
            timer.start();
        }

        protected void nextImage() {
            if (images.length > 0) {
                imageIndex++;
                if (imageIndex >= images.length) {
                    imageIndex = 0;
                }
                try {
                    BufferedImage image = ImageIO.read(images[imageIndex]);
                    label.setIcon(new ImageIcon(image));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

有关详细信息,请查看How to use Swing Timers

如果您的图片很大或来自慢速服务(例如通过互联网),您可以考虑使用SwingWorker,这样您就可以在后台执行长时间运行或阻止操作(关闭EDT),但更容易将更新同步到EDT。有关详细信息,请查看Refresh Image in JLabel with Timer