如何设置特定时间的jbutton图标以使其在单击时显示,以及如何在时间结束后消失该图标 [英] how to set jbutton icon for specific time to show when it is clicked and how to disappear the icon when time finished

查看:55
本文介绍了如何设置特定时间的jbutton图标以使其在单击时显示,以及如何在时间结束后消失该图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击时,如何将Java设置计时器代码设置为我的按钮,以设置按钮图标,以及在我的按钮图标必须显示然后该图标必须设置为空的情况下,处理过程会有一定的延时.

how to code java for set timer to my button for set button icon when i clicked and some time delay for process while my button icon has to show then the icon have to set null for time end.

我尝试过以下方法,但是当我不单击按钮时它是可行的

i have tried following way but it is work when i am not to clicked the button

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         


  timer = new Timer(5000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(chromeShown) {
                    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/r/ajaxloading.gif")));
                    chromeShown = true;
                } else {
                    jButton3.setIcon(null);
                    chromeShown = false;
                }
            }
        });
        timer.start();

        this.getContentPane().add(JButton);
        this.setVisible(true);

推荐答案

根据我对问题的理解,您的逻辑有些偏斜,应该按照类似的步骤进行操作

Based on my understanding of your problem, your logic is a little skewed, the actions should following along something like

单击按钮->图标已更改->计时器已启动...(正在等待)...->计时器已触发->图标已更改.

Button Clicked -> Icon Changed -> Timer Started ...(waiting)... -> Timer triggered -> Icon Changed.

此刻,您正在尝试更改Timer中图标的初始状态,这没有任何意义.我想您想做更多类似的事情...

At the moment, you're trying to change the initial state of the icon in the Timer, which doesn't make sense. I think you want to do something more like this...

click.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        click.setEnabled(false);
        click.setText("I'm running >>");
        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                click.setText("I be done");
                click.setEnabled(true);
            }
        });
        timer.start();
    }
});

基本上,当单击按钮时,这将设置按钮的文本并禁用按钮(因此您不能再次单击它),然后在1秒钟后,它将更改文本并启用按钮

Basically, when the button is clicked, this sets the button's text and disables the button (so you can't click it again) and the after 1 second, it changes the text and enables the button

这篇关于如何设置特定时间的jbutton图标以使其在单击时显示,以及如何在时间结束后消失该图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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