如何在单击按钮时添加秒表计时器?

时间:2018-05-17 12:44:35

标签: java swing user-interface

我在java中创建一个简单的GUI程序,当我点击“开始”按钮时,秒表应该在我创建的JLabel中开始。我尝试使用Timer Swing,但它只显示标签中“HH:mm:ss”的实时时间,这不是我想要的。

是否有任何功能可以实现秒表而不是显示实时计时器?

以下是代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.Timer;

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



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 Stopbtn = new JButton("STOP");
          JButton Reset = new JButton("RESET");
          JLabel time = new JLabel("Time shows here");
             panel.add(Startbtn);
             panel.add(Stopbtn);
            panel.add(Reset);
            panel.add(time);
             Startbtn.setBounds(50, 150, 100, 35);
             Stopbtn.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);


             Startbtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Timer timer = new Timer(1,new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {

                            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                            time.setText(format.format(new java.util.Date()));
                        }
                    });
                timer.start();
                }
            });

2 个答案:

答案 0 :(得分:1)

您可以使用java.time api存储点击的即时消息以及现在和点击之间的持续时间,如下所示:

Startbtn.addActionListener(new ActionListener(){

        Instant start;

        @Override
        public void actionPerformed(ActionEvent e) {
            start = Instant.now();

            Timer timer = new Timer(1, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    time.setText(Duration.between(start, Instant.now()).getSeconds() + "");
                }
            });
            timer.start();
        }
    });

相关部分是Duration.between(start, Instant.now()).getSeconds()。 您还可以使用getHours,getNanos,getMillis等格式化字符串,例如:

duration.getHours() + ":" + duration.getSeconds() + ":" + duration.getNanos();

您还可以在此处获取更多信息:https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

答案 1 :(得分:0)

启动计时器时实例化

Date beginnig = new Date();

然后,当您绘制标签而不是

time.setText(format.format(new java.util.Date()));

写点赞

Date current = new Date();
time.setText(format.format(new java.util.Date(current.getTime()-beginning.getTime())));

它可能有点粗糙但它应该有用。