动态更改jButton图标 [英] Dynamically change jButton icon

查看:48
本文介绍了动态更改jButton图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以检测某些计算机何时在线,并创建带有绿色在线"图标的按钮来显示此信息.我想添加功能以定期检查此机器是否仍在线,如果不是,请将图标更改为我已经定义的离线"图标.

I have a program that detects when certain machines are online and creates a button with a green "online" icon to show this. I want to add the functionality to check periodically if this machine is still online, and if it's not, change the icon to the "offline" icon that I've already defined.

推荐答案

我知道如何设置图标,但是我找不到解决方法 一旦按钮已经显示

I know how to set the icon, however I can't figure out a way to do it once the button has already been displayed

Swing的一致性可能有问题,这意味着所有Swing代码都必须在EDT上完成

probably you have issues with Concurency in Swing, that means that all Swing code must be done on EDT

然后,例如,必须将myButton.setIcon(myIcon)包装到invokeLater()

then you have to wrap myButton.setIcon(myIcon) to the invokeLater(), for example

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        myButton.setIcon(myIcon);
    }
});

这篇关于动态更改jButton图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆