欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-28 16:32:22
本文实例讲述了java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下: 应用名称:java计时器 用到的知识:java gui编程 开发环境:win8+ec...

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

应用名称:java计时器

用到的知识:java gui编程

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

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

效果图:

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

源代码:

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)转换工具:

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

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

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