如何在java中使用动画gif文件作为初始屏幕?
我正在使用Java1.5,希望在启动应用程序之前显示动画,并在加载应用程序时关闭动画。我在谷歌搜索后创建了一些东西,但如果我使用的不是动画gif,则动画gif不会显示动画 是否有人知道问题出在哪里,并知道如何解决问题?如何在java中使用动画gif文件作为初始屏幕?,java,swing,splash-screen,animated-gif,Java,Swing,Splash Screen,Animated Gif,我正在使用Java1.5,希望在启动应用程序之前显示动画,并在加载应用程序时关闭动画。我在谷歌搜索后创建了一些东西,但如果我使用的不是动画gif,则动画gif不会显示动画 是否有人知道问题出在哪里,并知道如何解决问题? public class SplashFrameSwing extends JFrame{ JPanel contentPane; JLabel imageLabel = new JLabel(); public SplashFrameSwing(String url) thr
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.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);
}
});
在该示例中,您的两个示例之间有一个更大的区别,即动画示例是一个远程URL。您是否尝试过将
verify_anim.gif
保存到硬盘上,以确保问题出在动画gif本身,而不是它存储在远程服务器上?是的,我是作为本地文件保存的,但结果相同。这适用于1.5版。