简体   繁体   English

动画gif无法播放-鼠标侦听器-输入了事件鼠标

[英]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) {}
        }
  1. Don't use a 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) 答案
  2. Don't attempt to load the image 'as needed', but instead load them & set them up on the button when it is initialized. 不要尝试“按需”加载图像,而是在初始化时加载并在按钮上进行设置。
  3. Change 更改
    getClass().getResource("images\\\\button_1_hover.gif") to getClass().getResource("images\\\\button_1_hover.gif")
    getClass().getResource("/images/button_1_hover.gif")
  4. Change the method used to load the image. 更改用于加载图像的方法。 ImageIO will not typically load an animated GIF correctly. ImageIO通常不会正确加载动画GIF。 See Show an animated BG in Swing for details. 有关详细信息,请参见在Swing中显示动画BG
  5. Change code of the form 更改表格的代码
    catch (Exception e) { .. to catch (Exception e) { ..
    catch (Exception e) { e.printStackTrace(); // very informative! ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM