而是使用:
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