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

是否有某种方法可以阻止动画播放?我正在考虑分配 gif 的静态 jpg,然后当我悬停时,分配动画 gif,但我认为没有移除鼠标的事件,MouseMotionListener因此我可以加载静态 jpg。

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) {
}
4

2 回答 2

5

看:

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

EG 在这个例子中,我没有添加一个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。

于 2012-10-13T17:59:27.160 回答
-1
button.setIcon(new ImageIcon("/*icon location*/"));
button.setRolloverIcon(new ImageIcon("/*icon location*/"

当鼠标指针移到按钮上时,动画 gif 图像不会变得不可见。

于 2014-11-07T10:39:59.093 回答