【发布时间】:2013-10-22 07:12:14
【问题描述】:
在我的代码中,我有一个 JButton(名为 PlotStatus),当单击它时,它会在 JLabel 中导入和打印图像。图像作为 BufferedImage 从不断更新的 png 文件导入。我希望能够在任何时候单击 JButton 时刷新/更新 JLabel 上的图像。也就是说,在每次单击时,我都希望缓冲存储器重置并 JLabel 重新加载(更新的)图像。相反,我打印出所有不同的图像,第一个留在前景中。
StatusLabel 和 temp 在这里定义为私有(但 public 没有区别)。 我尝试了许多不同的方法并阅读了许多绝对没有运气的帖子,因为看起来它应该有效。任何建议都非常感谢。
提前致谢。
//This is an Item Listener which reacts to clicking on the JButton PlotStatus
PlotStatus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JLabel statusLabel = new JLabel(); panel1.remove(statusLabel);
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new File("status.png"));
bufferedImage.flush();
JLabel temp = new JLabel();
temp.setIcon(new ImageIcon(bufferedImage));
statusLabel = temp;
panel1.add(statusLabel);
revalidate();repaint();
statusLabel.setVisible(true);statusLabel.setBounds(560,20,650,440);
} catch(IOException ex) {ex.printStackTrace();}
}
});
【问题讨论】:
-
我在这里应用了(第一个)答案的建议stackoverflow.com/questions/8084115/…,但它再次不起作用。
标签: jbutton jlabel bufferedimage