Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能示例
本文實(shí)例講述了Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能。分享給大家供大家參考,具體如下:
應(yīng)用名稱:Java計(jì)時(shí)器
用到的知識(shí):Java GUI編程
開發(fā)環(huán)境:win8+eclipse+jdk1.8
功能說明:計(jì)時(shí)功能,精確到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;
/**
* 計(jì)時(shí)器
*/
public class Timer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
// 計(jì)數(shù)線程
private CountingThread thread = new CountingThread();
// 記錄程序開始時(shí)間
private long programStart = System.currentTimeMillis();
// 程序一開始就是暫停的
private long pauseStart = programStart;
// 程序暫停的總時(shí)間
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("繼續(xù)");
}
}
};
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(); // 計(jì)數(shù)線程一直就運(yùn)行著
}
// 為窗體面板添加邊框
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);
}
// 配置標(biāo)簽
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.dbjr.com.cn 計(jì)時(shí)器");
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);
}
}
}
// 將毫秒數(shù)格式化
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:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計(jì)算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《java日期與時(shí)間操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java編程之多線程死鎖與線程間通信簡單實(shí)現(xiàn)代碼
這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡單實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
SpringBoot配置 Druid 三種方式(包括純配置文件配置)
本文給大家分享在項(xiàng)目中用純 YML(application.yml 或者 application.properties)文件、Java 代碼配置 Bean 和注解三種方式配置 Alibaba Druid 用于監(jiān)控或者查看 SQL 狀況的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-10-10
SpringSecurity+Mysql數(shù)據(jù)庫實(shí)現(xiàn)用戶安全登錄認(rèn)證的實(shí)踐
Spring Security 是一個(gè)提供身份認(rèn)證、授權(quán)和防范常見攻擊的安全權(quán)限框架,本文主要介紹了SpringSecurity+Mysql數(shù)據(jù)庫實(shí)現(xiàn)用戶安全登錄認(rèn)證的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
Struts1簡介和入門_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Struts1簡介和入門的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
詳解springboot shiro jwt實(shí)現(xiàn)權(quán)限管理
為什么使用jwt呢,因?yàn)榭梢酝ㄟ^URL,POST參數(shù)或者在HTTP header發(fā)送,因?yàn)閿?shù)據(jù)量小,傳輸速度也很快。本篇通過具體代碼來進(jìn)行詳情解析,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
詳解Java實(shí)現(xiàn)負(fù)載均衡的幾種算法代碼
本篇文章主要介紹了詳解Java實(shí)現(xiàn)負(fù)載均衡的幾種算法代碼 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

