2012-07-06 70 views
0

我正在使用java 1.5,並希望在啓動我的應用程序之前顯示一個動畫,並在加載應用程序時關閉它。我在Googleing之後創建了一些內容,但是如果我使用的不是動畫GIF,則動畫GIF不會生成動畫。如何在java中使用動畫gif文件作爲啓動畫面?

請問有什麼問題,並知道如何做到這一點?

public class SplashFrameSwing extends JFrame{ 
JPanel contentPane; 
JLabel imageLabel = new JLabel(); 

public SplashFrameSwing(String url) throws IOException { 
    try { 
     ImageIcon ii = new ImageIcon(url); 
     setBackground(new Color(0, 0, 0, 0)); 
     setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
     contentPane = (JPanel) getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 
     setUndecorated(true); 
     setSize(new Dimension(ii.getIconWidth(),ii.getIconHeight())); 
     imageLabel.setIcon(ii); 
     contentPane.add(imageLabel, java.awt.BorderLayout.CENTER); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 

public static void main(String... args){ 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       // URL url = new URL("http://i34.photobucket.com/albums/d129/nirendaran/Speed/verify_anim.gif"); 
       String url = "C:\\Users\\TV\\Pictures\\Icon\\sniffer-prev.gif"; 
       SplashFrameSwing splash = new SplashFrameSwing(url); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
} 
+0

在這個例子中,你的兩個例子之間有一個更大的區別,因爲動畫的是一個遠程URL。您是否嘗試將'verify_anim.gif'保存到您的硬盤驅動器,以確保問題是動畫GIF本身,而不是它存儲在遠程服務器上的事實? – 2012-07-06 08:38:19

+0

是的,我做了一個本地文件,但結果相同。 – itro 2012-07-06 09:29:08

+0

這個[示例](http://stackoverflow.com/a/5444422/230513)適用於1.5。 – trashgod 2012-07-06 09:47:31

回答

0

這適用於我,但我使用的版本是1.7.0_05。

EventQueue.invokeLater(new Runnable(){ 
    public void run(){ 
    JLabel label = new JLabel(); 

    label.setHorizontalAlignment(SwingConstants.CENTER); 

    try{ 
     // URL imageURL = new URL("http://static.ak.fbcdn.net/rsrc.php/v2/yb/r/GsNJNwuI-UM.gif"); 
     URL imageURL = new URL("http://i34.photobucket.com/albums/d129/nirendaran/Speed/verify_anim.gif"); 

     label.setIcon(new ImageIcon(imageURL)); 
    } 
    catch (MalformedURLException ex){ 
     ex.printStackTrace(); 
    } 

    JFrame frame = new JFrame(); 
    frame.add(label, BorderLayout.CENTER); 
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    frame.setMinimumSize(new Dimension(160, 120)); 
    frame.setVisible(true); 
    } 
});