I want to use animated GIF images the application using JFC/Swing tookkit. I am confused about this toolkit

痴心易碎 提交于 2019-12-25 18:12:27

问题


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();
    }
}

回答1:


This code (which is functionally equivalent to the code seen above) works on Windows 10 using Java 1.8.

Check it runs as you expected on your machine. If so, it would seem that snoopy_dancing.gif is not in the location that the code expects.

import java.awt.*;
import java.net.URL;
import javax.swing.*;

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 Font("Comic Sans MS", Font.BOLD, 16));
            headerLabel.setText("Your job crashed during the save process!");
            contentPane.add(headerLabel, BorderLayout.NORTH);
            // add the image label
            ImageIcon ii = new ImageIcon(
                    // this.getClass().getResource("snoopy_dancing.gif")); 
                    new URL("https://i.stack.imgur.com/OtTIY.gif"));
            imageLabel.setIcon(ii);
            contentPane.add(imageLabel, BorderLayout.CENTER);
            // show it
            this.setLocationRelativeTo(null);

            this.setVisible(true);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}


来源:https://stackoverflow.com/questions/42281418/i-want-to-use-animated-gif-images-the-application-using-jfc-swing-tookkit-i-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!