【问题标题】:fix the showing gif in Java swing [duplicate]修复 Java swing 中显示的 gif [重复]
【发布时间】: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 类。

标签: java swing gif


【解决方案1】:

在这种情况下,您 ImageIcon 将只显示 GIF 的第一帧。

您可以将 GIF 添加到 JLabel,您可以将其添加到 JPanel。检查this 以获得进一步的帮助。

【讨论】:

  • 如何在没有ImageIcon和Icon的情况下将GIF添加到JLable?
  • @ali 为什么?有没有引起什么问题?
猜你喜欢
  • 1970-01-01
  • 2015-12-12
  • 2017-05-26
  • 2018-03-09
  • 1970-01-01
  • 2012-10-31
  • 2013-03-22
  • 2015-01-04
  • 2017-12-28
相关资源
最近更新 更多