Java 在 jPanel 上显示动画 gif

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4339029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:08:16  来源:igfitidea点击:

Display animated gif on jPanel

java

提问by Yawn

In the overridden function for my JFrame:

在我的重写函数中JFrame

@Override
protected void paintComponent(Graphics g) {

    BufferedImage imagePerson;
    try {
        imagePerson = ImageIO.read(new File("errol.gif"));
    } catch (IOException e) {
        imagePerson = null;
    }

    g.drawImage(imagePerson, i * increment, j * increment - 1, null);
}

How can I change this so the animation on the gif is shown (without using threading). I have spent many hours trying to get this to work but to no avail.

如何更改此设置以便显示 gif 上的动画(不使用线程)。我花了很多时间试图让它发挥作用,但无济于事。

回答by aioobe

You could use an ImageIconfor this purpose. Have a look hereand here. If you need the animation on a JPanel, simply add a JLabel(with the ImageIcon) to the panel.

您可以ImageIcon为此目的使用 。看看这里这里。如果您需要 上的动画JPanel,只需向面板添加一个JLabel(带有ImageIcon)。

回答by Mehdi

Check this example for displaying an animated gif in Swing. you dont have to use any threads:

检查此示例以在 Swing 中显示动画 gif。您不必使用任何线程:

Icon imgIcon = new ImageIcon(this.getClass().getResource("ajax-loader.gif"));
JLabel label = new JLabel(imgIcon);
label.setBounds(668, 43, 46, 14); // for example, you can use your own values
frame.getContentPane().add(label);

Source

来源