简体   繁体   English

在jPanel上显示动画gif

[英]Display animated gif on jPanel

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). 如何更改此设置以显示gif上的动画(不使用线程)。 I have spent many hours trying to get this to work but to no avail. 我花了很多时间试图让它工作,但无济于事。

You could use an ImageIcon for this purpose. 您可以使用ImageIcon来实现此目的。 Have a look here and here . 看看这里这里 If you need the animation on a JPanel , simply add a JLabel (with the ImageIcon ) to the panel. 如果需要JPanel上的动画,只需将JLabel (带有ImageIcon )添加到面板即可。

Check this example for displaying an animated gif in Swing. 检查此示例以在Swing中显示动画gif。 you dont have to use any threads: 你不必使用任何线程:

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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM