Java实现的计时器【秒表】功能示例
作者:不能吃的坚果 时间:2023-11-10 21:24:50
本文实例讲述了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.aspxhome.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:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/C_jian/article/details/50506759
标签:Java,计时器,秒表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现语音识别代码
2022-06-03 03:26:33
![](https://img.aspxhome.com/file/2023/8/137928_0s.jpg)
深入理解Java中的final关键字_动力节点Java学院整理
2022-05-05 23:18:24
C#实现观察者模式(Observer Pattern)的两种方式
2023-06-20 21:05:18
![](https://img.aspxhome.com/file/2023/0/66130_0s.png)
Java为何需要平衡方法调用与内联
2023-01-02 20:37:22
![](https://img.aspxhome.com/file/2023/7/71207_0s.png)
基于Android FileProvider 属性配置详解及FileProvider多节点问题
2023-06-02 23:36:14
有关Java中的BeanInfo介绍
2023-08-05 00:13:42
![](https://img.aspxhome.com/file/2023/5/57955_0s.jpg)
手写Java LockSupport的示例代码
2021-11-05 07:00:39
![](https://img.aspxhome.com/file/2023/9/119049_0s.png)
Java基础元注解基本原理示例详解
2022-08-09 23:21:16
![](https://img.aspxhome.com/file/2023/4/67984_0s.png)
SpringBoot集成RabbitMQ和概念介绍
2022-06-25 18:36:24
![](https://img.aspxhome.com/file/2023/0/128850_0s.png)
SQL语句删除和添加外键、主键的方法
2023-04-16 22:18:35
解决C#中取消方向键对控件焦点控制的实现方法
2021-09-13 08:02:29
Flutter 构建一个常用的页面框架
2022-10-02 02:54:19
![](https://img.aspxhome.com/file/2023/0/114370_0s.jpg)
Android ToggleButton 详解及实例代码
2022-09-11 03:58:02
![](https://img.aspxhome.com/file/2023/5/125355_0s.png)
Android之PreferenceActivity应用详解
2023-03-28 14:26:37
![](https://img.aspxhome.com/file/2023/9/138199_0s.jpg)
Java List 用法实例详解
2022-11-29 05:27:20
android实现筛选菜单效果
2022-10-22 12:41:05
![](https://img.aspxhome.com/file/2023/7/123347_0s.gif)
Java重写与重载之间的区别
2021-06-30 03:16:13
C++类常量和类枚举
2022-05-05 17:07:47
![](https://img.aspxhome.com/file/2023/6/109786_0s.jpg)
Android 文件数据存储实例详解
2023-07-28 17:08:03
SpringBoot整合ElasticSearch实践
2023-02-05 06:02:08
![](https://img.aspxhome.com/file/2023/1/78281_0s.png)