[英]JLabel refresh icon with updated image
I'm trying to make an experiment in image manipulation. 我正试图在图像处理方面做一个实验。 Basically I have an image that is continously updated by a timer and i display that image in a JLabel. 基本上我有一个图像,由计时器不断更新,我在JLabel中显示该图像。
My problem is that JLabel does'nt refresh the image. 我的问题是JLabel没有刷新图像。
Here is my timer code: 这是我的计时器代码:
Timer timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
count++;
System.out.println("timer");
System.out.println(filename);
ImageIcon icon = new ImageIcon(filename);
label = new JLabel();
label.setIcon(icon);
label.setText(""+count);
panel = new JPanel();
panel.add(label);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.repaint();
frame.validate();
try{
FileWriter fstream;
fstream = new FileWriter(filename,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("text to append");
out.close();
}catch (Exception ex){
System.err.println("Error: " + ex.getMessage());
}
}
});
Where filename is path to my image. 其中filename是我的图像的路径。
Image is displayed but JLabel never refresh my image. 显示图像但JLabel从不刷新我的图像。 I tested my code and is working if I swich between two different images... 我测试了我的代码,如果我在两个不同的图像之间切换,它正在工作......
EDIT: 编辑:
I solved by duplicate every time last image created and renaming with a timestamp. 每次创建最后一个图像并使用时间戳重命名时,我都会通过复制解决。
label = new JLabel();
label.setIcon(icon);
label.setText(""+count);
panel = new JPanel();
panel.add(label);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.repaint();
frame.validate();
Replace all that with something like: 用以下内容替换所有内容:
label.setIcon(icon);
If the label is not visible at that point, declare it as a class attribute of the outer class or at the same level as the frame
(which is obviously accessible in that snippet). 如果标签在该点不可见,则将其声明为外部类的类属性或与frame
处于同一级别(显然可在该代码段中访问)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.