gpt4 book ai didi

java - 删除按钮之前更改颜色

转载 作者:行者123 更新时间:2023-11-30 05:11:32 25 4
gpt4 key购买 nike

我正在尝试创建一个按钮,该按钮会更改背景颜色,然后在设定的时间后将其自身从 JFrame 中删除,但它不会更改颜色,而是在等待期间保持按下状态。

public void actionPerformed(ActionEvent e) {
setBackground(Color.red);
try{
Thread.sleep(10000);
}
catch (InterruptedException iE) {

}
frame.remove(this);
}

有人能看出我做错了什么吗?

最佳答案

您的 sleep 发生在主 UI 线程中,因此按钮一直处于按下状态。如果你想要 sleep ,你应该创建一个新线程,让它进入休眠状态,然后从该线程中你可以获取框架来删除按钮。

new Thread() {
public void run() {
try {
Thread.sleep(10000);
// Now do what is needed to remove the button.
} catch (InterruptedException e) {
e.printStackTrace();
}

};
}.start();

关于java - 删除按钮之前更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com