如何更改JLabel的图标? [英] How to change icon of a JLabel?

查看:155
本文介绍了如何更改JLabel的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jlabel来显示生成的图像。但它只是第一次有效。之后,jlabel的imageicon不会改变。可能是什么问题?

I have a jlabel to show a generated image. But it only works the first time. After that, imageicon of the jlabel does not change. What could be the problem?

推荐答案

很可能你有两个JLabel实例。一个是类变量,一个是已添加到GUI的实例变量。问题是你的代码正在更新类变量。

Chance are that you have two instances of the JLabel. One is a class variable and one is an instance variable which has been added to the GUI. The problem is your code is updating the class variable.

或者,如果你没有更新EDT上的图标,你可能会遇到问题。

Or, maybe if you don't update the icon on the EDT you might have problems.

编辑:重新阅读问题。如果您正在谈论需要从文件重新加载的生成的图像,那么您需要摆脱缓存的图像。两种方法:

Just reread the question. If you are talking about a "generated image" that needs to be reloaded from a file, then you need to get rid of the cached image. Two ways to do this:

//  Using ImageIO

String imageName = "timeLabel.jpg";
imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );

//  Or you can flush the image

String imageName = "timeLabel.jpg";
ImageIcon icon = new ImageIcon(imageName);
icon.getImage().flush();
imageLabel.setIcon( icon );

如果您需要更多帮助,请发布 SSCCE

If you need more help post your SSCCE.

这篇关于如何更改JLabel的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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