我試圖在簡單的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;
}
}
有誰知道爲什麼我得到這個流關閉?
在此先感謝!
你是如何使用/調用這個類? –
'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)中的圖像進行熱鏈接。 –
本課是單身人士。我使用'LoadingGif.getInstance.runUI()'在我的main()的一開始就調用這個類,然後在最後我調用'LoadingGif.getInstance.stopLoading()' 事情是圖像很好加載,而gif動畫可能爲0.5s,然後我得到了錯誤信息。 – Jooooris