简体   繁体   English

如何使用动画 gif 作为 JFrame 标题栏图标

[英]how use animated gif as JFrame title bar icon

Is it possible to use an animated.gif image as the icon of a JFrame?是否可以使用 animated.gif 图像作为 JFrame 的图标?

example:例子:

public class myFrame extends JFrame
{
    java.net.URL imgURL = getCLass().getResource("/icons/AnimatedGif.gif");
    ImageIcon icon = new ImageIcon(imgURL);

    this.setIconImage(icon.getImage());
    icon.setImageObserver(this);

    ...
}

This method has not worked for me.这种方法对我不起作用。 The application hangs prior to making the JFrame visible.应用程序在使 JFrame 可见之前挂起。 Everything works fine though with a regular.gif icon.尽管使用 regular.gif 图标一切正常。

I tried a while back to have the icon of my JFrame animated just by setting the icon image to an animated gif.不久前,我尝试通过将图标图像设置为动画 gif 来使我的 JFrame 的图标具有动画效果。 I could never get that to work.我永远无法让它发挥作用。 I have come up with a work around though.不过,我想出了一个解决方法。 No guarantees about correctness or thread safety.不保证正确性或线程安全。

The basic idea is to have a separate thread that handles the icon animation. This thread's job is to constantly set the frame's icon image.基本思想是有一个单独的线程来处理图标 animation。这个线程的工作是不断地设置框架的图标图像。

This is a demo frame:这是一个演示框架:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class FrameWithAnimatedIcon extends JFrame
{
public static void main(String[] args)
{
    final FrameWithAnimatedIcon frame = new FrameWithAnimatedIcon();
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                frame.setVisible(true);
            } catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });

    IconAnimator animator = new IconAnimator(frame, Images.images, 250);
    animator.run();
}

public FrameWithAnimatedIcon()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);

}
}

This is the icon animator class:这是图标动画师 class:

import java.awt.Image;
import java.util.ArrayList;
import javax.swing.JFrame;

public class IconAnimator
{

JFrame           frame = null;
ArrayList<Image> images;
long             msBetweenImages;

public IconAnimator(JFrame frame, ArrayList<Image> images, long msBetweenImages)
{
    this.frame = frame;
    this.images = images;
    this.msBetweenImages = msBetweenImages;
}

public void run()
{
    while(true)
    {
        for(Image image : images)
        {
            try
            {
                frame.setIconImage(image);

                Thread.sleep(msBetweenImages);

            } catch(InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(frame == null)
            {
                return;
            }
        }
    }
}
}    

With this IconAnimator class, I can give it the target frame, a list of images, and the time between images, and it will animate the JFrame icon.有了这个 IconAnimator class,我可以给它目标帧、图像列表和图像之间的时间,它会为 JFrame 图标设置动画。 I imagine this code is probably not "best practices" material, however, it works.我想这段代码可能不是“最佳实践”材料,但是它确实有效。 A side note on implementation, I made a separate class called Images that just loads my images into an ArrayList. Each image is 16x16.关于实现的旁注,我制作了一个名为 Images 的单独 class,它只是将我的图像加载到 ArrayList 中。每个图像都是 16x16。 The list declaration of that class looks like this: class 的列表声明如下所示:

public static ArrayList<Image> images = new ArrayList<Image>(){{
   add(Toolkit.getDefaultToolkit().getImage(
       Images.class.getResource("/toolbarButtonGraphics/development/Bean16.gif")));

   add(Toolkit.getDefaultToolkit().getImage(
           Images.class.getResource   ("/toolbarButtonGraphics/development/Application16.gif"))); 

   add(Toolkit.getDefaultToolkit().getImage(
           Images.class.getResource("/toolbarButtonGraphics/development/Applet16.gif"))); 

   add(Toolkit.getDefaultToolkit().getImage(
           Images.class.getResource("/toolbarButtonGraphics/development/WebComponent16.gif"))); 
}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM