在jPanel上显示动画gif

Yaw*_*awn 8 java

在我的重写函数中JFrame:

@Override
protected void paintComponent(Graphics g) {

    BufferedImage imagePerson;
    try {
        imagePerson = ImageIO.read(new File("errol.gif"));
    } catch (IOException e) {
        imagePerson = null;
    }

    g.drawImage(imagePerson, i * increment, j * increment - 1, null);
}
Run Code Online (Sandbox Code Playgroud)

如何更改此设置以显示gif上的动画(不使用线程).我花了很多时间试图让它工作,但无济于事.

aio*_*obe 5

你可以用它ImageIcon来达到这个目的.看看这里这里.如果您需要a上的动画JPanel,只需在面板中添加JLabel(带ImageIcon).

  • 将标签添加到面板.然后移动标签你只需调用setLocation().无需重写paintComponent()方法或调用repaint(). (2认同)