Animated gif not playing - mouse listener - entered mouse

I have a button that I replaced with an image, when I freeze I want the image to play an animated gif. I added a mouse listener and entered the code to change the image in the gif. The image changes to gif; however gif does not revive. I looked at the previous answers on this site, there are few of them, but no one could 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) {} } 
+2
source share
1 answer
  • Do not use MouseListener for this, just set the icon using, setPressedIcon(Icon) , setRolloverIcon(Icon) , etc. See this answer for an example.
  • Do not try to download the image β€œas needed”, but instead load them and set them to a button during initialization.
  • Change getClass().getResource("images\\button_1_hover.gif") to getClass().getResource("/images/button_1_hover.gif")
  • Change the image upload method. ImageIO usually does not load animated GIFs. See Animated BG in Swing for more details.
  • Change catch (Exception e) { .. form code to catch (Exception e) { e.printStackTrace(); // very informative! .. catch (Exception e) { e.printStackTrace(); // very informative! ..
+4
source

Source: https://habr.com/ru/post/1500153/


All Articles