有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从一开始就重新启动gif动画?JFrames

我试图让一个gif在按下按钮时从动画的中间开始重新启动。我该怎么做

我尝试过以下方法:

lblDeath.setIcon(new ImageIcon(MainGame.class.getResource("/azzets/blank.png")));
lblDeath.setIcon(new ImageIcon(MainGame.class.getResource("/azzets/3 Seconds.gif")));
lblDeath.setVisible(false);
lblDeath.setIcon(new ImageIcon(MainGame.class.getResource("/azzets/3 Seconds.gif")));
lblDeath.setVisible(true);

(3秒gif是我试图重新启动的gif)

谢谢


共 (1) 个答案

  1. # 1 楼答案

    实际上,正如评论部分所述,您可以使用flush()来显示标签的图标图像:

    ImageIcon icon = (ImageIcon) gifLabel.getIcon();
    Image img = icon.getImage();
    ImageIcon newIcon = new ImageIcon(img);
    img.flush();
    gifLabel.setIcon(newIcon);