动画gif无法播放 - 鼠标侦听器 - 输入事件鼠标 [英] Animated gif does not play - mouse listener - event mouse entered

查看:124
本文介绍了动画gif无法播放 - 鼠标侦听器 - 输入事件鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用图像替换的按钮,在悬停时我希望图像能够播放GIF动画。我添加了一个鼠标监听器并输入了将图像更改为gif的代码。图像变为gif;然而,gif没有动画。我已经在这个网站上找到了以前的答案,但很少有人能够提供帮助。

I have a button that I have replaced with an image, on hovering I want the image to play an animated gif. I have added a mouse listener and entered the code for changing the image to the gif. The image changes to the gif; however the gif does not animate. 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. 为此,请不要使用 MouseListener ,只需使用
    设置图标 setPressedIcon(Icon)
    setRolloverIcon(Icon) 等。参见这个答案的例子。

  2. 不要试图根据需要加载图像,而是加载他们&在初始化按钮时将它们设置在按钮上。

  3. 更改
    getClass()。getResource(images \\button_1_hover.gif) to
    getClass()。getResource(/ images / button_1_hover.gif)

  4. 更改用于加载图像的方法。 ImageIO通常不会正确加载动画GIF。有关详细信息,请参见在Swing中显示动画BG

  5. 更改表单代码
    catch(例外e){..
    catch(例外e){e.printStackTrace(); //非常有用! ..

  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.
  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")
  4. Change the method used to load the image. ImageIO will not typically load an animated GIF correctly. See Show an animated BG in Swing for details.
  5. Change code of the form
    catch (Exception e) { .. to
    catch (Exception e) { e.printStackTrace(); // very informative! ..

这篇关于动画gif无法播放 - 鼠标侦听器 - 输入事件鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆