I just load an animated GIF images as an ImageIcon
.
The image of my Swing program running on Window 2000 PC
Note that I am using Java/JDK 1.4.x release.
Here is code, but it is not working:
/**
* DevDaily.com
* A sample program showing how to use an animated gif image
* in a Java Swing application.
*/
package giftest;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class MainFrame extends JFrame {
JPanel contentPane;
JLabel imageLabel = new JLabel();
JLabel headerLabel = new JLabel();
public MainFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
setSize(new Dimension(400, 300));
setTitle("Your Job Crashed!");
// add the header label
headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
headerLabel.setText(" Your job crashed during the save process!");
contentPane.add(headerLabel, java.awt.BorderLayout.NORTH);
// add the image label
ImageIcon ii = new ImageIcon(this.getClass().getResource(
"snoopy_dancing.gif")); imageLabel.setIcon(ii);
contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
// show it
this.setLocationRelativeTo(null);
this.setVisible(true)
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
new MainFrame();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…