Java:ImageIcon-图像文件已更新,但Java框架中的图像图标未更新 [英] Java: ImageIcon - image file updating but image icon in Java frame not
问题描述
我在JFrame
(Java GUI)的Jlabel
中有一个ImageIcon
.
I have got a ImageIcon
in a Jlabel
in a JFrame
(Java GUI).
ImageIcon
应该通过按下 Calculate 按钮(即calcButton.addActionListener(new ActionListener()
)并使用方法中的部分代码来更新:
The ImageIcon
should get updated based on pressing a Calculate button (i.e. calcButton.addActionListener(new ActionListener()
) with part of the code in the method:
icon2 = new ImageIcon("M:\\Repos\\rtrans\\radTransPlot.png");
Plot1.setIcon(icon2);
frame.add(Plot1,gc);
frame.setVisible(true);
开头的ImageIcon
(icon1)为空:
public class RadTransGui
{
private ImageIcon icon1 = new ImageIcon("M:\\Repos\\rtrans\\radTransPlotEmpty.png");
private ImageIcon icon2;
private JLabel Plot1 = new JLabel(icon1);
,并会根据第一次按下 Calculate 按钮进行正确更新,但不会在随后按下 Calculate 按钮后更新.每次按下 Calculate 时, M:\ Repos \ rtrans \ radTransPlot.png 的内容都会正确更新.
and gets properly updated based on the first Calculate button press but not after subsequent presses of Calculate button. The contents of M:\Repos\rtrans\radTransPlot.png gets updated correctly each time Calculate is pressed.
我尝试将ImageIcon设置为null,并在每次按下 Calculate 按钮时在帧中添加和删除JLabel.
I have tried setting the ImageIcon to null and adding and removing the JLabel to the frame each time the Calculate button is pressed.
有什么想法吗?谢谢.
推荐答案
ImageIcon()的构造函数在内部使用Toolkit.getDefaultToolkit().getImage
.
The constructor of ImageIcon() internally uses Toolkit.getDefaultToolkit().getImage
.
您必须手动使用Toolkit.getDefaultToolkit().createImage
而不是Toolkit.getDefaultToolkit().getImage
.后者使用缓存,而前者不使用缓存,并且总是返回一个新实例.
You have to manually use Toolkit.getDefaultToolkit().createImage
instead of Toolkit.getDefaultToolkit().getImage
. The latter uses cache whereas the former doesn't and always returns a new instance.
new ImageIcon(Toolkit.getDefaultToolkit().createImage("..filename.."))
来自createImage
的javadoc:
From the javadoc of createImage
:
返回的Image是一个新对象,不会与该方法或其getImage变体的任何其他调用方共享.
The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.
与getImage
的javadoc进行比较:
Compare with the javadoc of getImage
:
基础工具包尝试将具有相同文件名的多个请求解析为相同的返回图片. [...]如果指定文件中包含的图像数据发生更改,则从该方法返回的 Image对象可能仍包含过时信息,该信息是在先前调用后从文件中加载的.
The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. [...] If the image data contained in the specified file changes, the Image object returned from this method may still contain stale information which was loaded from the file after a prior call.
似乎没有任何Javadoc或规范规定ImageIcon应该使用缓存的图像,因此,如果您不知道100%正在做什么,那么这是一个非常脆弱的编程的完美示例.即使它在一种环境中工作也不能保证它总是能工作.
There seems to be no javadoc or spec that prescribes that ImageIcon should use cached images, so it's a perfect example of how fragile programming is if you don't know 100% what you're doing. Even if it works in one environment doesn't guarantee it always works.
这篇关于Java:ImageIcon-图像文件已更新,但Java框架中的图像图标未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!