【问题标题】:JLabel not showing .gif animationJLabel 不显示 .gif 动画
【发布时间】:2012-09-15 08:20:49
【问题描述】:

我正在尝试将“.gif”图像放在标签上。图像加载,但它是静态的(无动画)。任何猜测为什么?以及如何解决?

我正在使用的代码:

BufferedImage img1=ImageIO.read(TCPServer.class.getResource("filecopy.gif"));
JLabel filetransferpic = new JLabel(new ImageIcon(img1));

注意:我不想使用..

JLabel filetransferpic = new JLabel(new ImageIcon("G:\\filecopy.gif")); 

..方法。因为在这种方法中,我必须提供驱动器路径并将图像放在驱动器中。我想要项目文件夹“src”中的图像。

【问题讨论】:

标签: java swing jlabel gif bufferedimage


【解决方案1】:

在您的示例中,您使用的是BufferedImage。相反,请使用ImageIcon,如下所示:

ImageIcon gifImage = new ImageIcon(new URL("file:/Path/To/file.gif"));
JLabel yourLabel = new JLabel(gifImage);

这应该是动画的。但请记住,构造函数 URL(String) 会抛出 MalformedURLException;一定要抓住它。

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多