Java實現(xiàn)的計時器【秒表】功能示例
本文實例講述了Java實現(xiàn)的計時器【秒表】功能。分享給大家供大家參考,具體如下:
應(yīng)用名稱:Java計時器
用到的知識:Java GUI編程
開發(fā)環(huán)境: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";
// 計數(shù)線程
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("繼續(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(); // 計數(shù)線程一直就運行著
}
// 為窗體面板添加邊框
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("m.fzitv.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);
}
}
}
// 將毫秒數(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:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《java日期與時間操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
ssm框架controller層返回json格式數(shù)據(jù)到頁面的實現(xiàn)
這篇文章主要介紹了ssm框架controller層返回json格式數(shù)據(jù)到頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Struts 2 數(shù)據(jù)校驗功能及校驗問題的解決方案
這篇文章主要介紹了Struts 2 數(shù)據(jù)校驗功能及校驗問題的解決方案的相關(guān)資料,需要的朋友可以參考下2016-09-09

