图标是否已缓冲?

问题描述

当我单击“显示对话框”按钮时,会出现一个 jdialog,其中包含一个带有图标(图像)的 JLabel,如果文件“C:\Test\Test.png”存在,则图像显示正确。我有一个外部进程,白天更新此图像(始终使用相同的名称“Test.png”)。但我的问题是,即使我处理了 jdialog 并更新了图像(应用程序仍在运行),当我再次单击“显示对话框”时,jdialog 与旧图像一起显示(看起来旧图像在缓冲区中)在某处)。即使我关闭 jdialog,删除文件 e 再次打开它显示图像。

有什么办法可以预防吗?

我需要的是,当我打开 jdialog 时,它会显示更新后的图像。

import java.awt.Dimension;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.jdialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ShowImage implements Runnable {

    @Override
    public void run() {
        createGUI();
    }

    public void createGUI() {
        JButton jButton = new JButton("Show dialog");
        jButton.addActionListener((e) -> new ShowImageWindow());

        JFrame jFrame = new JFrame();
        jFrame.add(jButton);
        jFrame.pack();
        jFrame.setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);
        jFrame.setLocationRelativeto(null);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokelater(new ShowImage());
    }
}

class ShowImageWindow extends jdialog {
 
    private JLabel imagemJLabel;

    public ShowImageWindow() {
        initComponents();
    }
    
    public void initComponents() {
        Icon preview = new ImageIcon("C:\\Test\\Test.png");
        System.out.println(preview.toString());
        imagemJLabel = new JLabel();
        imagemJLabel.setHorizontalAlignment(SwingConstants.CENTER);
        imagemJLabel.setIcon(preview);
        
        JPanel contentPane = new JPanel();
        contentPane.add(this.imagemJLabel);
        
        setSize(new Dimension(500,500));
        setModal(true);
        setLocationRelativeto(null);
        setDefaultCloSEOperation(jdialog.disPOSE_ON_CLOSE);
        setContentPane(contentPane);
        setVisible(true);
    }
}

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)