【问题标题】:How to use animated gif file as a splash screen in java?如何在java中使用动画gif文件作为启动画面?
【发布时间】:2012-07-06 08:31:51
【问题描述】:

我正在使用 java 1.5 并希望在启动我的应用程序之前显示一个动画并在我的应用程序加载时关闭它。我在谷歌搜索后创建了一些东西,但如果我使用未显示的动画 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();
            }
        }
    });
}
}

【问题讨论】:

  • 在那个例子中,你的两个例子之间有一个更大的区别,因为动画是一个远程 URL。您是否尝试过将verify_anim.gif 保存到您的硬盘,以确保问题出在动画 GIF 本身,而不是它存储在远程服务器上?
  • 是的,我作为本地文件做了,但结果相同。
  • example 适用于 1.5。

标签: java swing splash-screen animated-gif


【解决方案1】:

这对我有用,但我使用的是 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);
  }
});

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2017-06-15
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多