Java Swing 中的 gif 动画问题 [英] Issue with gif animation in Java swing

查看:59
本文介绍了Java Swing 中的 gif 动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试加载 gif 动画时,我只是得到一个空帧.你知道有什么问题吗?该文件位于 extras/loading.gif 中:

package an1;导入 javax.swing.ImageIcon;导入 javax.swing.JFrame;导入 javax.swing.JLabel;公共类 An1 {公共静态无效主(字符串 [] 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);}}

我使用本网站

解决方案

在您的机器上运行此源代码并检查它是否有效..

import java.net.*;导入 javax.swing.*;公共类 An1 {public static void main(String[] args) 抛出 MalformedURLException {JFrame frame = new JFrame("Test");ImageIcon 加载 = 新的 ImageIcon(新网址("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);}}

注意应用程序资源将在部署时成为嵌入式资源,因此明智的做法是立即开始访问它们.必须访问 通过 URL 而不是文件.请参阅信息.嵌入资源的页面,了解如何形成 URL.

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:

解决方案

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆