[英]Animated gif does not play - mouse listener - event mouse entered
I have a button that I have replaced with an image, on hovering I want the image to play an animated gif. 我有一个已替换为图像的按钮,悬停时我希望图像播放动画gif。 I have added a mouse listener and entered the code for changing the image to the gif. 我添加了一个鼠标侦听器,并输入了将图像更改为gif的代码。 The image changes to the gif; 图像变为gif; however the gif does not animate. 但是gif不会动画。 I have had a look for previous answers on this site, there are few but none have been able to help. 我已经在该站点上查找过以前的答案,很少,但没有一个能够提供帮助。
@Override
public void mouseEntered(MouseEvent arg0) {
try
{
Image img = ImageIO.read(getClass().getResource("images\\button_1_hover.gif"));
btnShip1.setIcon(new ImageIcon(img));
}
catch (IOException ex) {}
}
MouseListener
for this, just set up the icon using, setPressedIcon(Icon)
, setRolloverIcon(Icon)
etc. See this answer for an example. 不要为此使用MouseListener
,只需使用setPressedIcon(Icon)
, setRolloverIcon(Icon)
等来设置setRolloverIcon(Icon)
。有关示例,请参setRolloverIcon(Icon)
答案 。 getClass().getResource("images\\\\button_1_hover.gif")
to getClass().getResource("images\\\\button_1_hover.gif")
到 getClass().getResource("/images/button_1_hover.gif")
catch (Exception e) { ..
to catch (Exception e) { ..
至 catch (Exception e) { e.printStackTrace(); // very informative! ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.