如何在java中使用动画gif文件作为初始屏幕?

时间:2012-07-06 08:31:51

标签: java swing splash-screen animated-gif

我正在使用java 1.5,想要在启动应用程序之前显示动画,并在加载应用程序时关闭它。我在google之后创建了一些内容,但如果我使用的不是GIF动画,那么动画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();
            }
        }
    });
}
}

1 个答案:

答案 0 :(得分: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);
  }
});