首页 > 解决方案 > java - 如何用java中的另一个替换屏幕上的gif?

问题描述

我想打开加载中的gif 5秒,然后切换到另一个gif,但是我无法切换

我尝试了 frame.dispose()、Panel.removeAll() 和许多其他函数

        setSize(1366, 768);
        setLocation(0, 0);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        ImageIcon image = new ImageIcon("C:\\Users\\user\\Desktop\\hmi-loading.gif");
        JLabel imagelabel = new JLabel(image);
        imagelabel.setBackground(Color.BLACK);
        imagelabel.setForeground(Color.BLACK);
        p.setBackground(Color.BLACK);
        p.add(imagelabel);
        getContentPane().add(p);
        p.removeAll();
        frame.removeAll();
        p.setBackground(Color.BLACK);
        ImageIcon imagee = new ImageIcon("C:\\Users\\user\\Desktop\\kAi.gif");
        JLabel imagelabel1 = new JLabel(image);
        imagelabel1.setBackground(Color.BLACK);
        imagelabel1.setForeground(Color.BLACK);
        p.setBackground(Color.BLACK);
        p.add(imagelabel1);
        getContentPane().add(p);

那是一个 gif .. 我如何添加一个计时器以便这个计时器被另一个替换?

我希望 gif 被另一个替换。gif打开,仅此而已。

标签: javaswingloading

解决方案


只是一个基于Swing Timer API 的简单示例。

public class ImageView extends JFrame {

    private JPanel panel;
    private JLabel label;
    private Timer timer;

    public ImageView() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(640, 480);

        panel = new JPanel(new BorderLayout());
        label = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\hmi-loading.gif"));
        panel.add(label);

        timer = new Timer(5000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {

                label.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\kAi.gif"));
            }
        });

        timer.start();

        this.getContentPane().add(panel);
        this.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new ImageView());
    }
}

我现在没有足够的时间来测试它,但是这个最小的片段应该可以工作。


推荐阅读