我想在使用JFC/Swing套件的应用程序中使用动画GIF图像.我对此工具包感到困惑 [英] I want to use animated GIF images the application using JFC/Swing tookkit. I am confused about this toolkit

查看:79
本文介绍了我想在使用JFC/Swing套件的应用程序中使用动画GIF图像.我对此工具包感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将动画GIF图像加载为ImageIcon. 我在Windows 2000 PC上运行的Swing程序的图像 请注意,我正在使用Java/JDK 1.4.x版本.

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

推荐答案

此代码(在功能上等同于上面的代码)在使用Java 1.8的Windows 10上运行.

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

检查它是否在计算机上按预期运行.如果是这样,则snoopy_dancing.gif似乎不在代码预期的位置.

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

这篇关于我想在使用JFC/Swing套件的应用程序中使用动画GIF图像.我对此工具包感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆