欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

java jlable添加gif,Java动画GIF而不使用JLabel

发布时间:2024/9/19 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java jlable添加gif,Java动画GIF而不使用JLabel 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Is there a way to display an animated GIF image in Java without using a JLabel? I'm trying to implement some GIFS in a game and would like to just paint them without needing to mess with JComponents. Would an image observer work? Am I out of luck?

解决方案

Following shows a image in JPanel without using JLabel:

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class ImagePanel extends JPanel

{

Image image;

public ImagePanel()

{

image = Toolkit.getDefaultToolkit().createImage("e:/java/spin.gif");

}

@Override

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if (image != null)

{

g.drawImage(image, 0, 0, this);

}

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable()

{

@Override

public void run()

{

JFrame frame = new JFrame();

frame.add(new ImagePanel());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 400);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

}

总结

以上是生活随笔为你收集整理的java jlable添加gif,Java动画GIF而不使用JLabel的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。