Java设置延迟更改imageicon

Java设置延迟更改imageicon

本文介绍了Java设置延迟更改imageicon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置按下按钮的延迟时间,以将图像图标设置为某个图像,然后设置另一个延迟,以便设置另一个图像,所有这些都可以通过单击来完成.换句话说:

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();
    }
}

推荐答案

  1. 不要添加 MouseListener JButton ,也不要为 mouseClicked()添加 ActionListener 代替,所有 Mouse Key 事件都在 JButton API 并正确

  1. don't add MouseListener to JButton, nor for mouseClicked(), add ActionListener instead, btw all Mouse and Key events are implemented in JButton 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:21