问题描述
我正在尝试设置按下按钮的延迟时间,以将图像图标设置为某个图像,然后设置另一个延迟,以便设置另一个图像,所有这些都可以通过单击来完成.换句话说:
i'm trying to set a delay when a button is pressed to set an imageicon to a certain image then set another delay so that another image would be set, all of this by single click.in other word :
单击按钮->设置图像->延迟->设置另一张图像.
click a button->set image->delay->set another image.
我在代码中得到的只是设置另一个图像"的最后状态.
what i get in my code is the last state only "set another image".
我也不想使用计时器,我想使用延迟.
also i don't want to use use timers, i want to use delays.
这是我关注的代码部分.
and here the part in my code i'm concerned about.
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lblNewLabel.setIcon(and);
sleeep(500);
lblNewLabel.setIcon(app);
}
});
这是延迟函数
void sleeep(int n)
{
try {
Thread.sleep(n);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
推荐答案
-
不要添加
MouseListener
到JButton
,也不要为mouseClicked()
添加ActionListener
代替,所有Mouse
和Key
事件都在JButton API
并正确
don't add
MouseListener
toJButton
, nor formouseClicked()
, addActionListener
instead, btw allMouse
andKey
events are implemented inJButton API
and correctly
不要使用 Thread.sleep(n);
,您遇到 Swing中的一致性,请使用 Swing Timer
,
don't to use Thread.sleep(n);
you have an issue with Concurency in Swing, use Swing Timer
instead,
这篇关于Java设置延迟更改imageicon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!