Java实现的计时器【秒表】功能示例

 更新时间:2019年02月25日 10:11:33   作者:不能吃的坚果  
这篇文章主要介绍了Java实现的计时器【秒表】功能,结合实例形式分析了Java结合JFrame框架的计时器功能相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下:

应用名称:Java计时器

用到的知识:Java GUI编程

开发环境:win8+eclipse+jdk1.8

功能说明:计时功能,精确到1毫秒,可暂停。

效果图:

源代码:

import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 计时器
 */
public class Timer extends JFrame {
  /**
 *
 */
 private static final long serialVersionUID = 1L;
 private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
  // 计数线程
  private CountingThread thread = new CountingThread();
  // 记录程序开始时间
  private long programStart = System.currentTimeMillis();
  // 程序一开始就是暂停的
  private long pauseStart = programStart;
  // 程序暂停的总时间
  private long pauseCount = 0;
  private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
  private JButton startPauseButton = new JButton("开始");
  private JButton resetButton = new JButton("清零");
  private ActionListener startPauseButtonListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      if (thread.stopped) {
        pauseCount += (System.currentTimeMillis() - pauseStart);
        thread.stopped = false;
        startPauseButton.setText("暂停");
      } else {
        pauseStart = System.currentTimeMillis();
        thread.stopped = true;
        startPauseButton.setText("继续");
      }
    }
  };
  private ActionListener resetButtonListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      pauseStart = programStart;
      pauseCount = 0;
      thread.stopped = true;
      label.setText(INITIAL_LABEL_TEXT);
      startPauseButton.setText("开始");
    }
  };
  public Timer(String title) throws HeadlessException {
    super(title);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocation(300, 300);
    setResizable(false);
    setupBorder();
    setupLabel();
    setupButtonsPanel();
    startPauseButton.addActionListener(startPauseButtonListener);
    resetButton.addActionListener(resetButtonListener);
    thread.start(); // 计数线程一直就运行着
  }
  // 为窗体面板添加边框
  private void setupBorder() {
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.setContentPane(contentPane);
  }
  // 配置按钮
  private void setupButtonsPanel() {
    JPanel panel = new JPanel(new FlowLayout());
    panel.add(startPauseButton);
    panel.add(resetButton);
    add(panel, BorderLayout.SOUTH);
  }
  // 配置标签
  private void setupLabel() {
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
    this.add(label, BorderLayout.CENTER);
  }
  // 程序入口
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      e.printStackTrace();
    }
    Timer frame = new Timer("www.jb51.net 计时器");
    frame.pack();
    frame.setVisible(true);
  }
  private class CountingThread extends Thread {
    public boolean stopped = true;
    private CountingThread() {
      setDaemon(true);
    }
    @Override
    public void run() {
      while (true) {
        if (!stopped) {
          long elapsed = System.currentTimeMillis() - programStart - pauseCount;
          label.setText(format(elapsed));
        }
        try {
          sleep(1); // 1毫秒更新一次显示
        } catch (InterruptedException e) {
          e.printStackTrace();
          System.exit(1);
        }
      }
    }
    // 将毫秒数格式化
    private String format(long elapsed) {
      int hour, minute, second, milli;
      milli = (int) (elapsed % 1000);
      elapsed = elapsed / 1000;
      second = (int) (elapsed % 60);
      elapsed = elapsed / 60;
      minute = (int) (elapsed % 60);
      elapsed = elapsed / 60;
      hour = (int) (elapsed % 60);
      return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);
    }
  }
}

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

您可能感兴趣的文章:

相关文章

  • idea创建springboot项目和springcloud项目的详细教程

    idea创建springboot项目和springcloud项目的详细教程

    这篇文章主要介绍了idea创建springboot项目和springcloud项目方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • 详解Spring Boot 项目启动时执行特定方法

    详解Spring Boot 项目启动时执行特定方法

    这篇文章主要介绍了详解Spring Boot 项目启动时执行特定方法,Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。感兴趣的小伙伴们可以参考一下
    2018-06-06
  • java操作mongodb时,对象bean和DBObject相互转换的方法(推荐)

    java操作mongodb时,对象bean和DBObject相互转换的方法(推荐)

    下面小编就为大家带来一篇java操作mongodb时,对象bean和DBObject相互转换的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Java实现的Windows资源管理器实例

    Java实现的Windows资源管理器实例

    这篇文章主要介绍了Java实现的Windows资源管理器,实例分析了基于java实现windows资源管理器的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • java 结合jQuery实现跨域名获取数据的方法

    java 结合jQuery实现跨域名获取数据的方法

    下面小编就为大家带来一篇java 结合jQuery实现跨域名获取数据的方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • 大厂禁止SpringBoot在项目使用Tomcat容器原理解析

    大厂禁止SpringBoot在项目使用Tomcat容器原理解析

    这篇文章主要为大家介绍了大厂禁止SpringBoot在项目使用Tomcat原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Java 时间日期详细介绍及实例

    Java 时间日期详细介绍及实例

    这篇文章主要介绍了Java 时间日期详细介绍及实例的相关资料,需要的朋友可以参考下
    2017-01-01
  • Spring Boot中单例类实现对象的注入方式

    Spring Boot中单例类实现对象的注入方式

    这篇文章主要介绍了Spring Boot中单例类实现对象的注入方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • java中BigDecimal的操作方法

    java中BigDecimal的操作方法

    这篇文章主要介绍了java中BigDecimal的操作方法,较为详细的分析了BigDecimal类在进行商业计算时的应用方法,精度以及注意事项等问题,需要的朋友可以参考下
    2014-12-12
  • Java怎么重命名 Amazon S3 中的文件和文件夹

    Java怎么重命名 Amazon S3 中的文件和文件夹

    在本文中,我们探讨了使用适用于 Java 的 AWS 开发工具包重命名 S3 存储桶中的文件和文件夹的方法,我们探索了两种不同的情况,它们使用相同的概念来重命名对象,用新名称复制它们并删除原始名称
    2023-10-10

最新评论