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

发布于 2013-08-16 11:05:46
似乎对我来说很好..。


我用了以下图标.(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();
}
});
}
}
}我刚意识到你在用一个非循环的礼物。这意味着你需要尝试和“重置”重新开始它的播放。
尝试使用类似于icon.getImage().flush();的东西,其中icon是您的ImageIcon。您必须将一个MouseListener附加到按钮以检测mouseEnter事件并重置ImageIcon.
https://stackoverflow.com/questions/18270701
复制相似问题