【问题标题】:ImageIcon do not update for a new image with the same URLImageIcon 不会针对具有相同 URL 的新图像进行更新
【发布时间】:2013-04-09 02:08:03
【问题描述】:

我想使用 JLabel(Icon) 显示来自我的网站 (http://xxx.xxx.xxx.xxx/java_pic/test.jpg) 的图像。 我有一个刷新按钮来新建一个新的 JLabel 和 ImageIcon(为了获得最新的图像) 程序运行成功...但是当我上传新图像以覆盖旧图像时(http://xxx.xxx.xxx.xxx/java_pic/test.jpg),我按下刷新按钮...什么也没发生! 我重新启动我的程序......现在出现了新图像......为什么? 当我再次新建 ImageIcon 时,它不应该从网站重新加载图像吗?

public void refresh(){
    URL iconUri = null;
    iconUri = new URL("http://XXX.XXX.XXX.XXX/java_pic/test.jpg");
    ImageIcon imageIcon = new ImageIcon(iconUri);
    JLabel imageLabel = new JLabel(imageIcon);
    frame.add(imageLabel);
    ...
    ...
}

当我点击刷新按钮时,它会调用 refresh()...为什么? 谢谢!

【问题讨论】:

    标签: java swing


    【解决方案1】:

    图像被缓存。刷新清除缓存:

    imageIcon.getImage().flush();
    

    【讨论】:

    • 非常感谢!这行得通!另一个问题是,我可以在“new JLabel(imageIcon);”之后立即刷新 imageIcon 吗?
    猜你喜欢
    • 2011-06-04
    • 2017-06-05
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多