【问题标题】:Animated ImageIcon as Button动画 ImageIcon 作为按钮
【发布时间】:2013-08-16 19:46:22
【问题描述】:

我有一个 imageIcon 作为按钮,现在我会在你翻转时为它设置动画。我尝试在 setRolloverIcon(Icon) 上使用动画 gif(无循环)。但是当我再次将鼠标悬停在按钮上时,gif 不再播放。当我使用循环 gif 时,它会从随机帧播放它。我尝试使用 paintComponent 将 Shape 或图像绘制为 Button,效果很好,但即使我使用 setPreferredSize() 或 setSize() 或 setMaximumSize() Button 使用其默认大小,如图所示(中间按钮)。我正在使用 GroupLayout,这可能是问题吗?

【问题讨论】:

  • 动画 gif 不起作用。您需要使用多个图像作为框架。
  • 我认为它需要像精灵一样的东西。好吧,我试过了,但按钮仍然使用它的默认大小,如图所示。

标签: java swing jbutton animated-gif imageicon


【解决方案1】:

对我来说似乎工作得很好......

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

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();
                }

            });
        }    
    }
}

我刚刚意识到您使用的是非循环 gif。这意味着您将需要尝试“重置”以重新开始播放。

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

【讨论】:

  • 请你测试我的代码,因为它不起作用,你或我也是
  • @mKorbel 不要使用button.setIcon(null),您可以尝试使用icon.getImage().flush(),这将强制JVM重新加载图标并再次开始播放...
  • 不是我试过flush(),试图覆盖BasicButtonUI,因为MetalLaF不起作用,缺少一些通知器
  • @mKorbel 似乎可以正常工作(尽我所能,手头没有单个循环 gif :P)
  • 对 SystemL&F 的权限需要 flush(),但仍然只能在 setIcon 端使用,setRollover 在 Win8/Java7 中什么也不做
猜你喜欢
  • 1970-01-01
  • 2015-03-11
  • 2015-07-09
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
相关资源
最近更新 更多