在jLabel上使用setIcon会重复旧图像 [英] Use of setIcon on jLabel repeats old image

查看:465
本文介绍了在jLabel上使用setIcon会重复旧图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示从网站下载的图片,使用 setIcon jLabel

I'm attempting to display an image that was downloaded from a website, with the use of setIcon and a jLabel

jLabel5.setIcon(new ImageIcon("image.png"));

在程序开始时,图像不存在,下载,之后显示,没有问题。但是如果它改变了,即使它下载了一个较新版本的图像,它也会显示旧图像,好像它有一个缓存或其他东西。

At the start of the program, the image doesn't exist, it gets downloaded, and after that displayed, with no problems. But if it changes, even if it downloads a newer version of the image, it will display the old one, as if it had a cache of it or something.

有人知道为什么会这样吗?如何使用或不使用此方法获得解决方法?

Does someone know why this happens? How to get a workaround with or without this method?

我还尝试执行以下操作以查看它是否有帮助,但没有成功:

I have also tried to do the following to see if it could help, with no success:

jLabel5.setIcon(null);
jLabel5.setIcon(new ImageIcon("image.png"));

它会显示任何内容,然后再显示相同的旧图像。

It would display nothing and then the same old image again.

推荐答案


它将显示旧的,好像它有一个缓存或其他东西。

it will display the old one, as if it had a cache of it or something.

是的,缓存是个问题。以下是几个选项:

Yep, caching is the problem. Here are a couple of options:

//  This works using ImageIO

imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );

//  Or you can flush the image

ImageIcon icon = new ImageIcon(imageName);
icon.getImage().flush();
imageLabel.setIcon( icon );

这篇关于在jLabel上使用setIcon会重复旧图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆