求JAVA的秒表源代码,有暂停等功能
纯Java做的秒表:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestTimer extends JFrame implements ActionListener, Runnable {
private static TestTimer obj;
private JButton btnStart;
private JButton btnPause;
private JButton btnResume;
private JButton btnStop;
private JLabel lblTime;
private static Thread th;
private long count;
public TestTimer(){
super("秒表");
btnStart = new JButton("开始");
btnPause = new JButton("暂停");
btnResume = new JButton("继续");
btnStop = new JButton("停止");
lblTime = new JLabel("00:00:00.000");
this.setLayout(new FlowLayout());
this.add(btnStart);
this.add(btnPause);
this.add(btnResume);
this.add(btnStop);
this.add(lblTime);
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnResume.addActionListener(this);
btnStop.addActionListener(this);
this.setSize(150, 200);
this.setVisible(true);
}
public static void main(String[] args) {
obj = new TestTimer();
}
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("开始")){
th = new Thread(obj);
count = 0;
th.start();
}
else if(btn.getText().equals("暂停")){
th.suspend();
}
else if(btn.getText().equals("继续")){
th.resume();
}
else if(btn.getText().equals("停止")){
th.stop();
}
}
@Override
public void run() {
while(true){
int ms, seconds, minutes, hours;
String msg = "";
hours = (int)(count / 3600000);
minutes = (int)((count - hours * 3600000) / 60000);
seconds = (int)((count - hours * 3600000 - minutes * 60000) / 1000);
ms = (int)(count % 1000);
if(hours < 10){
msg += "0" + hours + ":";
}
else{
msg += hours + ":";
}
if(minutes < 10){
msg += "0" + minutes + ":";
}
else{
msg += minutes + ":";
}
if(seconds < 10){
msg += "0" + seconds + ":";
}
else{
msg += seconds + ":";
}
if(ms < 10){
msg += "00" + ms;
}
else if(ms < 100){
msg += "0" + ms;
}
else{
msg += ms;
}
lblTime.setText(msg);
count++;
try {
Thread.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行界面:
2024-09-19 广告
<script language="javascript">
var _TimeID=null;
var _Str = "00:00:00:00";
var _H = 0; //小时初始值
var _mm = 0; //分钟初始值
var _ss = 0; //秒钟初始值
var _TimeCount=0;//毫秒初始值
function setMyTime()
{
_TimeCount++;
if(_TimeCount>=59){//判断毫秒是否大于或等于59毫秒
_TimeCount = 0;//条件成立,清除毫秒计时
_ss++//秒钟增加
if(_ss>=59){//判断秒钟是否大于或等于59秒
_ss = 0;//条件成立,清除秒钟计时
_mm++//分钟增加
if(_mm>=59){//判断分钟是否大于或等于59分
_mm = 0;//条件成立,清除分钟计时
_H++//小时增加
}
}
}
var _Hz = "";
var _mmz = "";
var _ssz = "";
var _TCz = "";
if(_H<10){_Hz = "0"}//小于两位数时,在前面加0
if(_mm<10){_mmz = "0"}
if(_ss<10){_ssz = "0"}
if(_TimeCount<10){_TCz = "0"}
_Str = _Hz+_H+":"+_mmz+_mm+":"+_ssz+_ss+":"+_TCz+_TimeCount
document.getElementById("sj").innerText=_Str;
}
function Sta()
{
_TimeID=setInterval("setMyTime();",1);
}
function Stop()
{
if(_TimeID==null) alert('秒表还没有开始');
else clearInterval(_TimeID);
}
function Clear()
{
_H = 0; //清除小时
_mm = 0; //清除分钟
_ss = 0; //清除秒钟
_TimeCount=0;//清除毫秒
_Str = "00:00:00:00";//清除变量
document.getElementById("sj").innerText=_Str;//赋值innerText到sj
if(_TimeID==null) alert('秒表还没有开始');
else clearInterval(_TimeID);
}
</script>