简体   繁体   中英

How can create timer with JLabel?

I want to display in my JPanel a JLabel with timer in this mode, for example:

03:50 sec
03:49 sec
....
....
00:00 sec

So I have build this code:

@SuppressWarnings("serial")
class TimeRefreshRace extends JLabel implements Runnable {

    private boolean isAlive = false;

    public void start() {
        Thread t = new Thread(this);
        isAlive = true;
        t.start();
    }

    public void run() {
        int timeInSecond = 185
        int minutes = timeInSecond/60;
        while (isAlive) {
            try {
                //TODO
            } catch (InterruptedException e) {
                log.logStackTrace(e);
            }
        }
    }

}//fine autoclass

And with this code, I can start the JLabel

TimeRefreshRace arLabel = new TimeRefreshRace ();
arLabel.start();

So I have the time in secondo for example 180 second, how can I create the timer?

You could within your try block call the Event Dispatcher Thread (EDT) and update your UI:

        try {
            SwingUtils.invokeLater(new Runnable() {
                @Override
                public void run() {
                    this.setText(minutes + " left");
                }
            }
            //You could optionally block your thread to update your label every second.
        }

Optionally, you could use a Timer instead of an actual thread, so your TimerRefreshRace will have its own timer which periodically fires an event. You would then use the same code within your try-catch block to update the UI.

Here is an example, how to build a countdown label. You can use this pattern to create your component.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class TimerTest {

    public static void main(String[] args) {
        final JFrame frm = new JFrame("Countdown");
        final JLabel countdownLabel = new JLabel("03:00");
        final Timer t = new Timer(1000, new ActionListener() {
            int time = 180;
            @Override
            public void actionPerformed(ActionEvent e) {
                time--;
                countdownLabel.setText(format(time / 60) + ":" + format(time % 60));
                if (time == 0) {
                    final Timer timer = (Timer) e.getSource();
                    timer.stop();
                }
            }
        });
        frm.add(countdownLabel);
        t.start();
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

    private static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM