首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画ImageIcon按钮

动画ImageIcon按钮
EN

Stack Overflow用户
提问于 2013-08-16 09:59:25
回答 1查看 3.4K关注 0票数 4

我有一个按钮的imageIcon,现在我会动画,当你滚。我尝试在setRolloverIcon(图标)上使用一个动画gif (没有循环)。但是当我再次按下按钮时,gif就不会再播放了。当我使用一个环形的gif,然后它从一个随机帧播放它。我尝试使用paintComponent将形状或图像绘制为Button,这很好,但即使使用setPreferredSize()或setSize()或setMaximumSize(),Button也使用其默认大小,正如您在图片(中间按钮)中看到的那样。我在使用GroupLayout,这可能是问题所在吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-16 11:05:46

似乎对我来说很好..。

我用了以下图标.(png和gif)..。

代码语言:javascript
复制
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AnimatedButton {

    public static void main(String[] args) {
        new AnimatedButton();
    }

    public AnimatedButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ImageIcon animatedGif;

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton btn = new JButton(new ImageIcon("WildPony.png"));
            btn.setRolloverEnabled(true);
            animatedGif = new ImageIcon("ajax-loader.gif");
            btn.setRolloverIcon(animatedGif);
            add(btn);

            btn.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    animatedGif.getImage().flush();
                }

            });
        }    
    }
}

我刚意识到你在用一个非循环的礼物。这意味着你需要尝试和“重置”重新开始它的播放。

尝试使用类似于icon.getImage().flush();的东西,其中icon是您的ImageIcon。您必须将一个MouseListener附加到按钮以检测mouseEnter事件并重置ImageIcon.

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18270701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档