且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

摇摆计时器不停止

更新时间:2023-12-04 08:23:40

它只是伪代码,以了解如何启动和停止计时器.

Its just pseudo code to see how timer can be started and stopped.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.Timer;

public class TimerOnJLabel extends JFrame {

    private static final long serialVersionUID = 1L;        
    long start = System.currentTimeMillis();
    long elapsedTimeMillis;
    int sec = 5;
    Timer timer;

    public TimerOnJLabel() {
        super("TooltipInSwing");
        setSize(400, 300);
        getContentPane().setLayout(new FlowLayout());
        final JLabel b1;
        final JRadioButton jrb = new JRadioButton();
        b1 = new JLabel("Simple tooltip 1");

        ActionListener timerTask = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                elapsedTimeMillis = System.currentTimeMillis();
                b1.setText("Timer : " + (elapsedTimeMillis-start)/1000+" ::::: " +sec);
                System.out.println("Timer working: " + sec);
                if(--sec == 0){
                    timer.stop();
                    System.out.println("Timer Stopped");
                }
            }
        };
        timer = new Timer(1000, timerTask);
        System.out.println("Timer Started");
        timer.start();

        getContentPane().add(b1);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);          
        setVisible(true);
    }

    public static void main(String args[]){
        new TimerOnJLabel();
    }
}

我希望有帮助.