use*_*852 2 java swing bufferedimage jlabel gif
我想在标签上放一个".gif"图像.图像加载,但它是静态的(没有动画).任何猜测为什么?以及如何解决?
我正在使用的代码:
BufferedImage img1=ImageIO.read(TCPServer.class.getResource("filecopy.gif"));
JLabel filetransferpic = new JLabel(new ImageIcon(img1));
Run Code Online (Sandbox Code Playgroud)
注意:我不想用..
JLabel filetransferpic = new JLabel(new ImageIcon("G:\\filecopy.gif"));
Run Code Online (Sandbox Code Playgroud)
..approach.因为在这种方法中,我必须给出驱动路径并将图像放在驱动器中.我想要项目文件夹"src"中的图像.
在您的示例中,您使用的是BufferedImage.相反,使用ImageIcon,像这样:
ImageIcon gifImage = new ImageIcon(new URL("file:/Path/To/file.gif"));
JLabel yourLabel = new JLabel(gifImage);
Run Code Online (Sandbox Code Playgroud)
这应该是动画的.但请记住,构造函数URL(String)会抛出一个MalformedURLException; 一定要抓住它.