【问题标题】:IOException Stream Closed on GifIOException 流在 Gif 上关闭
【发布时间】:2017-11-02 17:20:05
【问题描述】:

我试图在一个简单的 JFrame 上显示一个 Gif。我创建了“LoadingGif”类,但在 Gif 出现后一秒钟我收到了这个错误消息java.io.IOException : Stream closed,然后它就停止了。

我用LoadingGif.getInstance.runUI()调用这个类,这个类的源代码是:

public class LoadingGif {

    private static LoadingGif instance = null;

    private JFrame f;
    private JLabel label;
    private URL url;
    private ImageIcon imageIcon;

    private LoadingGif()
    {
         url = TraceaReq.class.getResource("/load.gif");
         imageIcon = new ImageIcon(url);
         label = new JLabel(imageIcon);
    }

    public void runUI()
    {
         f = new JFrame(RQTFGenDOORS.VERSION+" - Loading...");
         f.getContentPane().add(label);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setResizable(false);
         f.setVisible(true);
         f.setAlwaysOnTop(true);
    }

    public void stopLoading(){
        if(f.isDisplayable())
        {
            f.dispose();
        }
    }

    public static LoadingGif getInstance()
    {
        if(instance == null)
        {
            instance = new LoadingGif();
        }
        return instance;
    }
}

有人知道我为什么关闭了这个流吗?

提前致谢!

【问题讨论】:

  • 你如何使用/调用这个类?
  • imageIcon = new ImageIcon(url); label = new JLabel(imageIcon); 标签/图标将异步加载图像(加载时可能会发生其他事情),而此代码 if(f.isDisplayable()) { f.dispose(); 将导致 JVM 停止加载图像框架是可显示的。我怀疑这是问题所在。 1) 为了尽快获得更好的帮助,请发布minimal reproducible exampleShort, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。
  • 这个类是一个单例。我在 main() 的开头使用LoadingGif.getInstance.runUI() 调用这个类,然后在最后我调用LoadingGif.getInstance.stopLoading() 问题是图像加载良好,gif 动画可能持续 0.5 秒,然后我收到错误消息。

标签: java swing gif ioexception


【解决方案1】:

我首先尝试简化您的示例代码。例如,我刚刚使用了 GIF 文件的硬编码路径(它现在是硬编码的,以确保图像位置一切正常,并且类路径没有任何问题等)。我还使用了LoadingGif 类的构造函数,而不是单例工厂。

我的示例代码如下所示:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LoadingGif {

    private JFrame frame;
    private JLabel label;
    private ImageIcon imageIcon;

    private LoadingGif() {
        imageIcon = new ImageIcon("/home/me/Temp/loading-gif/src/animated-penguin-gif-5.gif");
        label = new JLabel(imageIcon);
    }

    public void runUI() {
        frame = new JFrame("MyGif - Loading...");
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
    }

    public static void main(String args[]) {
        LoadingGif loadingGif = new LoadingGif();
        loadingGif.runUI();
    }   
}

GIF 是动画并显示没有任何错误。

就我而言,GIF 文件并不大。正如 @Andrew Thompson 所说,可能在您的情况下它非常大并且需要一些时间来加载。无论如何,您可以通过javax.swing.ImageIcon#getImageLoadStatus 方法检查图像是否加载,然后检查返回的标志。对于java.awt.MediaTracker#COMPLETE Javadoc 说:

/**
 * Flag indicating that the downloading of media was completed
 * successfully.
 * @see         java.awt.MediaTracker#statusAll
 * @see         java.awt.MediaTracker#statusID
 */
public static final int COMPLETE = 8;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多