【发布时间】:2008-09-29 15:23:12
【问题描述】:
我正在尝试从存储在 jar 文件中的动画 gif 创建 ImageIcon。
ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));
图像加载,但仅加载动画 gif 的第一帧。动画不播放。
如果我从文件系统上的文件加载动画 gif,一切都会按预期工作。动画播放所有帧。所以这行得通:
ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");
如何将 gif 动画从 jar 文件加载到 ImageIcon 中?
编辑:这是一个完整的测试用例,为什么不显示动画?
import javax.imageio.ImageIO;
import javax.swing.*;
public class AnimationTest extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AnimationTest test = new AnimationTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});
}
public AnimationTest() {
super();
try {
JLabel label = new JLabel();
ImageIcon imageIcon = new ImageIcon(ImageIO.read(AnimationTest.class.getClassLoader().getResourceAsStream("animated.gif")));
label.setIcon(imageIcon);
imageIcon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
标签: java swing animated-gif javax.imageio