java swing 图片gif,Java swing中的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);

}

}

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:

8IXqb.gif

解决方案

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);

}

}

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值