JButton上的GIF动画,当鼠标悬停时播放 [英] Animated GIF on a JButton, play it when mouse is hovered

查看:152
本文介绍了JButton上的GIF动画,当鼠标悬停时播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Icon icon = new ImageIcon(getClass().getResource( "/img/icon.gif" ) );
aButton = new JButton("Its a button", icon);

是否存在某种可以阻止动画播放的方法? 我当时想分配一个gif的静态jpg,然后当我将鼠标悬停时分配一个动画的gif,但是我不认为在MouseMotionListener中有一个腾出鼠标的事件,因此我可以加载该静态jpg./p>

gif会在按钮中循环播放,但是,如果我将鼠标悬停在它上面,它将消失.

如果鼠标光标不在按钮上,如何使gif静态?

如果我使用MouseMotionListener,如果我取下鼠标会触发事件吗?

@Override
public void mouseMoved(MouseEvent e) {
//play the gif
//if I take mouse off, call some method to stop playing animated gif
}

@Override
public void mouseDragged(MouseEvent e) {
}

解决方案

请参阅:

无需设置显式鼠标侦听器,切换会自动发生.

E.G.在此示例中,我没有添加MediaTracker,因此将图像弹出到标签中以允许加载时间.最终用户是ImageObserver(请等到看到它旋转后再关闭第一个对话框).

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

public class ImageSwapOnButton {

    public static void main( String[] args ) throws Exception {
        URL url = new URL("http://1point1c.org/gif/thum/plnttm.gif");

        Image image = Toolkit.getDefaultToolkit().createImage(url);
        ImageIcon spinIcon = new ImageIcon(image);
        JOptionPane.showMessageDialog(null, new JLabel(spinIcon));

        // create a static version of this icon
        BufferedImage bi = new BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        g.drawImage(image,0,0,null);
        g.dispose();
        ImageIcon staticIcon = new ImageIcon(bi);

        JButton button = new JButton(staticIcon);
        button.setRolloverIcon(spinIcon);
        JOptionPane.showMessageDialog(null, button);
    }
}

此外,请勿将静态图像制作为JPEG. JPEG有损,并且不支持透明性.使用单帧GIF或PNG.

Icon icon = new ImageIcon(getClass().getResource( "/img/icon.gif" ) );
aButton = new JButton("Its a button", icon);

Is there some kind of method that can stop an animated from playing? I was thinking of assigning a static jpg of the gif, then when I hover, assign the animated gif, but I don't think there is an event for taking off mouse in MouseMotionListener so I can load back the static jpg.

The gif loops in the button, however, if I hover over it, it disappears.

How can I make the gif static if my mouse cursor is not on the button?

If I use MouseMotionListener, does it fire an event if I take off my mouse?

@Override
public void mouseMoved(MouseEvent e) {
//play the gif
//if I take mouse off, call some method to stop playing animated gif
}

@Override
public void mouseDragged(MouseEvent e) {
}

解决方案

See:

No need for setting an explicit mouse listener, the changeover happens automatically.

E.G. In this example I did not add a MediaTracker so popped the image into a label to allow for load time. The end user is the ImageObserver (wait till you see it spin before dismissing the first dialog).

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

public class ImageSwapOnButton {

    public static void main( String[] args ) throws Exception {
        URL url = new URL("http://1point1c.org/gif/thum/plnttm.gif");

        Image image = Toolkit.getDefaultToolkit().createImage(url);
        ImageIcon spinIcon = new ImageIcon(image);
        JOptionPane.showMessageDialog(null, new JLabel(spinIcon));

        // create a static version of this icon
        BufferedImage bi = new BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        g.drawImage(image,0,0,null);
        g.dispose();
        ImageIcon staticIcon = new ImageIcon(bi);

        JButton button = new JButton(staticIcon);
        button.setRolloverIcon(spinIcon);
        JOptionPane.showMessageDialog(null, button);
    }
}

Also, don't make the static image as JPEG. A JPEG is lossy and does not support transparency. Either use a single frame GIF or a PNG.

这篇关于JButton上的GIF动画,当鼠标悬停时播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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