有没有办法在JLabel中显示动画GIF图像,如JPEG或PNG图像?我想从URL加载动画GIF以在标签中显示它.
如果我尝试使用常见的静态图像方法,我只需要收到GIF的第一帧…
url = new URL("http://example.gif");
image = ImageIO.read(url);
ImageIcon icon = new ImageIcon(image);
picture = new JLabel();
picture.setIcon(icon);
解决方法:
而是使用:
ImageIcon icon = new ImageIcon(url);
简而言之,使用ImageIO加载动画GIF将创建静态GIF(由动画的第一帧组成).但是如果我们将URL传递给ImageIcon,它将正确加载动画的所有帧然后运行它们.
所以改变这个:
url = new URL("http://example.gif");
image = ImageIO.read(url);
ImageIcon icon = new ImageIcon(image);
picture = new JLabel();
picture.setIcon(icon);
对此:
url = new URL("http://example.gif");
ImageIcon icon = new ImageIcon(url); // load image direct from URL
picture = new JLabel(icon); // pass icon to constructor
甚至这个:
url = new URL("http://example.gif");
picture = new JLabel(new ImageIcon(url)); // don't need a reference to the icon
标签:java,image,gif,swing,jlabel
来源: https://codeday.me/bug/20190609/1204216.html
392

被折叠的 条评论
为什么被折叠?



