动画(循环)GIF可以用JLabelHTML格式显示(在格式化的文本组件中JEditorPane),并且可以循环显示.
但是要加载图像作为容器的背景,我通常会使用ImageIO.read()或者Toolkit.getImage()(后者在我感觉上一个千年怀旧时).加载图像的方法都不会导致循环图像,它通常只是第一帧.
如何为背景加载动画图像?
我正在尝试在JLabel中加载动画GIF.
虽然这有效:
URL urlsd;
try {
urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
ImageIcon imageIcon = new ImageIcon(urlsd);
JLabel progress = new JLabel(imageIcon);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
另一方面,这不,并且我不想从URL获取GIF,因为我已经拥有GIF.加载结果只显示GIF的第一帧:
try {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));
JLabel progress = new JLabel(imageIcon);
imageIcon.setImageObserver(progress);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我想这肯定是有原因的,但我找不到它.
谢谢!亚历克斯