Java:如何在ActionListener之后不断更新使用AtomInteger倒计时的JLabel [英] Java: How to continuously update JLabel which uses atomicInteger to countdown after ActionListener

查看:82
本文介绍了Java:如何在ActionListener之后不断更新使用AtomInteger倒计时的JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关该主题的不同主题,这些主题建议 Swing Timer类 SwingUtilities.InvokeLater

I was reading different threads on the subject which suggested the Swing Timer class or SwingUtilities.InvokeLater

...但是我在将他们的头缠住时遇到很多麻烦.

...but I am having a lot of trouble wrapping my head around them.

我使用 atomicInteger 创建了我的倒数计时器,它在控制台中运行良好.但是,当我尝试将其合并到Swing中时,它只会更新开始值和结束值(例如,设置5秒倒计时将显示在帧中:"5"-> 5秒后->"0".

I used atomicInteger to create my countdown timer and it works fine in the console. However, When I try to incorporate it in Swing, it only updates the starting and ending value (e.g. set a 5 sec countdown will display in the frame: "5" -> after 5 seconds -> "0".

我是否有任何简单的方法保持并刷新" atomicInteger倒数标签,或者唯一的方法是使用Swing Timer类?

Is there any simple way for me to keep and "refresh" my atomicInteger countdown label, or the only way is using the Swing Timer class?

感谢您的耐心!

ps.而不是做作业,只是想让自己成为自定义计时器来学习. (即拖延)

ps. not homework, just trying to make myself a custom timer to study. (ie. procrastinating)

我希望这堂课足够,如果您也需要框架/面板代码,请告诉我.

private class ClickListener implements ActionListener{

     public void actionPerformed(ActionEvent e){            
         int t_study = 5;
         atomicDown.set(t_study);

         if (e.getSource() == b_study){
             while(atomicDown.get() > 0){       
                t_study = atomicDown.decrementAndGet(); 
        l_studyTime.setText(Integer.toString(t_study));
             try {
                Thread.sleep(1000);
             }
             catch (InterruptedException e1) {
                 System.out.println("ERROR: Thread.sleep()");   
                 e1.printStackTrace();
             }
          }
     }
     else if(e.getSource() == b_exit){
         System.exit(0); 
     }
     else
         System.out.println("ERROR: button troll");
     }
}

推荐答案

将代码段转换为SSCCE之后,这就是我所得到的(似乎可以正常工作-以我对原始代码的理解为最佳).

After turning the code snippet into an SSCCE, this is what I get (which seems to work - as best as I understand the original code).

我没有更改变量名称.请了解常见的 Java命名约定 1 用于类,方法&属性名称和始终使用它.

I have not changed the variable names. Please learn common Java naming conventions1 for class, method & attribute names & use it consistently.

  1. 特定名称,例如b_study应该更像studyButton或类似名称.有些人会注意到,按钮"不应该是名称的一部分,但是当您同时具有同时具有学习"按钮和amp;的GUI时.标签,我看不到任何其他将它们分开的合乎逻辑的方法.
  1. Specifically names like b_study should be more along the lines of studyButton or similar. Some will note that 'button' should not be part of the name, but when you have a GUI with both a 'Study' button & label, I don't see any other logical way to separate them.


import java.awt.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.AtomicInteger;

class TimerTicker {

    public static final int STUDY_TIME = 15;
    AtomicInteger atomicDown = new AtomicInteger(STUDY_TIME);
    JButton b_study;
    JButton b_exit;
    JLabel l_studyTime;

    TimerTicker() {
        JPanel gui = new JPanel();

        b_study = new JButton("Study");
        ClickListener listener = new ClickListener();
        b_study.addActionListener(listener);
        gui.add(b_study);

        b_exit = new JButton("Exit");
        b_exit.addActionListener(listener);
        gui.add(b_exit);

        l_studyTime = new JLabel("" + atomicDown.get());
        gui.add(l_studyTime);

        JOptionPane.showMessageDialog(null, gui);
    }

    private class ClickListener implements ActionListener {

        Timer timer;

        public void actionPerformed(ActionEvent e){
            if (e.getSource() == b_study) {
                ActionListener countDown = new ActionListener() {

                    public void actionPerformed(ActionEvent ae) {
                        if (!(atomicDown.get() > 0)) {
                            timer.stop();
                            // reset the count.
                            atomicDown.set(STUDY_TIME);
                        } else {
                            l_studyTime.setText(
                                Integer.toString(
                                    atomicDown.decrementAndGet()));
                        }
                    }
                };
                timer = new Timer(1000,countDown);
                timer.start();
            } else if(e.getSource() == b_exit) {
                System.exit(0);
            } else {
                System.out.println("ERROR: button troll");
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TimerTicker();
            }
        });
    }
}

这篇关于Java:如何在ActionListener之后不断更新使用AtomInteger倒计时的JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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