Stop a stopwatch

后端 未结 4 1480
迷失自我
迷失自我 2020-11-27 08:36

I have the following code in a JPanel class which is added to a another class (JFrame). What I\'m trying to implement is some sort of a stopwatch program.

st         


        
4条回答
  •  -上瘾入骨i
    2020-11-27 09:10

    long startTime = System.currentTimeMillis();
        Timer timer = new Timer(100,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                long elapsedTime = System.currentTimeMillis()-startTime;
                long mil = elapsedTime%1000;
                long sec = elapsedTime/1000%60;
                long min = elapsedTime/60000%60;
                long hr = elapsedTime/3600000;
                label.setText(String.format("%02d:%02d:%02d.%03d", hr,min,sec,mil));
            }
        });
        timer.start();
    

提交回复
热议问题