【发布时间】:2017-11-06 06:04:36
【问题描述】:
我想在 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。
【问题讨论】:
-
该问题的答案有 ImagePanel,但它在 java 8 中不可用
-
ImagePanel 是作为该问题答案的一部分呈现的自定义类。它不是任何 Java 版本中的 Java SE 类。