2017-06-01 77 views
2

我試圖在簡單的JFrame上顯示一個Gif。我創建了「LoadingGif」類,但在Gif出現後,我收到了一秒鐘的錯誤消息java.io.IOException : Stream closed,然後停止。IOException流在Gif上關閉

我把這個類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; 
    } 
} 

有誰知道爲什麼我得到這個流關閉?

在此先感謝!

+1

你是如何使用/調用這個類? –

+1

'imageIcon = new ImageIcon(url); label = new JLabel(imageIcon);'標籤/圖標將異步加載圖像(加載時可能會發生其他事情),而此代碼if(f.isDisplayable()){f.dispose();'將只要框架可顯示,就會導致JVM停止加載圖像。我懷疑是這個問題。 1)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)獲取圖像的一種方法是通過[本問答](http://stackoverflow.com/q/19209650/418556)中的圖像進行熱鏈接。 –

+0

本課是單身人士。我使用'LoadingGif.getInstance.runUI()'在我的main()的一開始就調用這個類,然後在最後我調用'LoadingGif.getInstance.stopLoading()' 事情是圖像很好加載,而gif動畫可能爲0.5s,然後我得到了錯誤信息。 – Jooooris

回答

0

我試着先簡化你的示例代碼。例如,我剛剛使用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;