问题描述
f.e。我有一个电子邮件客户端,它收到新消息,带有传入消息的按钮开始执行某些操作,直到用户点击它以查看最新消息。
f.e. I have an email client, it receives new message, button with incoming messages starts doing something, until user clicks it to see whats up.
我正在尝试按钮通过选择,等待然后取消选择吸引注意力,但这没有任何作用!
I'm trying to make button attract attention by selecting, waiting and then deselecting it, but this does nothing!
do{
button.setSelected(true);
Thread oThread = new Thread() {
@Override
public void run() {
synchronized (this) {
try {
wait(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
button.setSelected(false);
}
};
oThread.start();
}while(true);
推荐答案
你应该使用Swing计时器。不要与外部线程的GUI对象交互。
You should use Swing timers for that. Don't interact with GUI objects from foreign threads.
Java教程中有一些文档:。
There's some docs in the Java tutorial: How to use Swing timers.
这是一个示例方法,你可以这样做使用按钮的图标。
Here's an example way you could do this playing with the button's icon.
// member var
Icon buttonIcon;
Timer timer;
// in constructor for example
buttonIcon = new ImageIcon("resources/icon.png");
button.setIcon(buttonIcon);
timer = new Timer(1000, this);
timer.start();
// in the actionPerformed handler
if (button.getIcon() == null)
button.setIcon(icon);
else
button.setIcon(null);
您的课程需要实施 ActionListener
这就像那样工作。添加一些逻辑以在需要时停止闪烁。
Your class will need to implement ActionListener
for this to work like that. Add some logic to stop the flashing when you need it.
这篇关于Java swing:选择/取消选择JButton来模仿脉冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!