我想在 java swing 中显示 gif。
对于显示 gif,我使用了这些代码中的内容,但在这两个代码中,gif 都没有移动,就像图像一样是静态的。
第一个代码:
void showGif() {
try {
JPanel panel = new JPanel();
BufferedImage bufferedImage = ImageIO.read(new File("address of gif");
Icon icon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(icon);
label.setVisible(true);
panel.add(label);
} catch (IOException e) {
e.printStackTrace();
}
}
第二个代码:
void showGif() {
try {
ImageIcon imageIcon;
BufferedImage bufferedImage = ImageIO.read(new File("address of gif"));
imageIcon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(imageIcon);
JPanel panel = new JPanel();
label.setVisible(true);
panel.add(label, TOP_ALIGNMENT);
} catch (IOException e) {
e.printStackTrace();
}
}
请帮我正确显示 gif。
最佳答案
在这种情况下,ImageIcon
将仅显示 GIF 的第一帧。
您可以将 GIF 添加到 JLabel,然后再将其添加到 JPanel。检查this寻求进一步的帮助。
关于java - 修复 Java swing 中显示 gif 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369386/