评论

收藏

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

编程语言 编程语言 发布于:2021-09-18 17:23 | 阅读数:219 | 评论:0

这篇文章主要介绍了Java实现的计时器【秒表】功能,结合实例形式分析了Java结合JFrame框架的计时器功能相关操作技巧,需要的朋友可以参考下
本文实例讲述了java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下:
应用名称:java计时器
用到的知识:java gui编程
开发环境:win8+eclipse+jdk1.8
功能说明:计时功能,精确到1毫秒,可暂停。
效果图:
DSC0000.gif

源代码:
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.zzvips.com 计时器");
  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)转换工具:https://tool.zzvips.com/t/timestamp/
在线秒表计时器:https://tool.zzvips.com/t/miaobiao/
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/C_jian/article/details/50506759

关注下面的标签,发现更多相似文章