Java swing中的gif动画问题 [英] Issue with gif animation in Java swing
问题描述
当我尝试加载gif动画时,我只是得到一个空白帧.你知道出什么事了吗?该文件位于extras/loading.gif中:
When I try to load a gif animation I am just getting an empty frame. Do you know what is wrong? The file is located within extras/loading.gif:
package an1;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class An1 {
public static void main(String[] args){
JFrame frame = new JFrame("Test");
ImageIcon loading = new ImageIcon("extras/loading.gif");
frame.add(new JLabel(loading, JLabel.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
我使用默认设置使用此网站 http://spiffygif.com 获得loading.gif.我只是将名称从gif.gif更改为loading.gif.在这里:
I got loading.gif using this website http://spiffygif.com with the default settings. I just changed the name from gif.gif to loading.gif. Here it is:
推荐答案
在计算机上运行此源,并检查其是否有效.
Run this source on your machine and check it works..
import java.net.*;
import javax.swing.*;
public class An1 {
public static void main(String[] args) throws MalformedURLException {
JFrame frame = new JFrame("Test");
ImageIcon loading = new ImageIcon(
new URL("http://i.stack.imgur.com/8IXqb.gif"));
frame.add(new JLabel(loading, JLabel.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
应用程序资源将在部署时变成嵌入式资源,因此明智的做法是立即开始访问它们.必须访问 embedded-resource 的问题通过URL而不是文件.请参阅信息.嵌入资源的页面以了解如何形成网址.
N.B. Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.
这篇关于Java swing中的gif动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!