在JLabel怪异中加载动画GIF

Ale*_*oVK 6 java swing jlabel animated-gif

我正在尝试在JLabel中加载动画GIF.

虽然这有效:

URL urlsd;
try {
    urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
    ImageIcon imageIcon = new ImageIcon(urlsd); 
    JLabel progress = new JLabel(imageIcon);    
    progress.setBounds(5, 20, 66, 66);
    contentPane.add(progress);
} catch (MalformedURLException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

另一方面,这不,并且我不想从URL获取GIF,因为我已经拥有GIF.加载结果只显示GIF的第一帧:

try {   
    ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));

    JLabel progress = new JLabel(imageIcon);
    imageIcon.setImageObserver(progress);
    progress.setBounds(5, 20, 66, 66);
    contentPane.add(progress);
} catch (MalformedURLException e) {

    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我想这肯定是有原因的,但我找不到它.

谢谢!亚历克斯

Xeo*_*eon 12

您可以尝试加载您的GIF文件:

public class Test extends JPanel {
    public Test() {
        ImageIcon imageIcon =
          new ImageIcon(Test.this.getClass().getResource("starzoom-thumb.gif"));
    }
}
Run Code Online (Sandbox Code Playgroud)

或者Test.class.getResouce()如果您的上下文是静态的则使用