将恢复功能添加到秒表

时间:2018-05-18 09:57:18

标签: java swing

我编写了一个带有三种功能的简单秒表。首先,我有一个开始秒表的开始按钮,一个暂停秒表暂停按钮,最后一个重置按钮来重置整个秒表。

当我按下暂停按钮时,秒表暂停,比如10.0秒。当我恢复秒表(再次按下“开始”按钮)时,秒表不会从10.0秒开始恢复。它从我暂停的时间和当前时间恢复。例如,如果我暂停5秒并点击恢复,则秒表从15.0秒开始。

我知道Swing.Timer中没有实际的暂停功能。是否有办法解决这个问题,以便秒表恢复正常?

任何建议都将不胜感激。

代码:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.Instant;
import javax.swing.Timer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class GuiStopwatch {





    public static void main(String[] args) {
        JFrame frame = new JFrame("Stopwatch");

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
          JPanel panel = new JPanel();

          panel.setLayout(null);

          JButton startbtn = new JButton("START");  
          JButton pausebtn = new JButton("PAUSE");
          JButton reset = new JButton("RESET");
          JLabel time = new JLabel("Time shows here");
             panel.add(startbtn);
             panel.add(pausebtn);
            panel.add(reset);
            panel.add(time);
             startbtn.setBounds(50, 150, 100, 35);
             pausebtn.setBounds(50, 200, 100, 35);
             reset.setBounds(50, 250, 100, 35);
             time.setBounds(50, 350, 100, 35);
             time.setBackground(Color.black);
             time.setForeground(Color.red);
             frame.add(panel);



            Timer timer = new Timer(1,new ActionListener() {
                Instant start = Instant.now();
                @Override
                public void actionPerformed(ActionEvent e) {


                    time.setText( Duration.between(start, Instant.now()).getSeconds() +  ":" + Duration.between(start, Instant.now()).getNano() );


                }
            });



             startbtn.addActionListener(new ActionListener() {


                @Override
                public void actionPerformed(ActionEvent e) {





                timer.start();




                }
            });



             pausebtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    timer.stop();

                }
            });



             reset.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                time.setText("0:0");

                }
            });

1 个答案:

答案 0 :(得分:3)

从概念上讲,这个想法是,你想要跟踪"总运行"秒表,它是所有活动的总持续时间。

您可以通过多种方式实现这一目标,其中一种方法可能就是保持一个仅在秒表停止或暂停时更新的运行总计。 "持续时间"然后,秒表是"当前持续时间的总和" "当前"循环和"前一个"持续时间

像...一样的东西。

public class StopWatch {
    private LocalDateTime startTime;
    private Duration totalRunTime = Duration.ZERO;

    public void start() {
        startTime = LocalDateTime.now();
    }

    public void stop() {
        Duration runTime = Duration.between(startTime, LocalDateTime.now());
        totalRunTime = totalRunTime.plus(runTime);
        startTime = null;
    }

    public void pause() {
        stop();
    }

    public void resume() {
        start();
    }

    public void reset() {
        stop();
        totalRunTime = Duration.ZERO;
    }

    public boolean isRunning() {
        return startTime != null;
    }

    public Duration getDuration() {
        Duration currentDuration = Duration.ZERO;
        currentDuration = currentDuration.plus(totalRunTime);
        if (isRunning()) {
            Duration runTime = Duration.between(startTime, LocalDateTime.now());
            currentDuration = currentDuration.plus(runTime);
        }
        return currentDuration;
    }
}

好的,startstop基本上与pauseresume相同,但您明白了。

并且,一个可运行的例子......

现在,此示例不断运行Swing Timer,但StopWatch可以随时pausedresumed,关键是要证明{{1}实际上工作正常;)

StopWatch