十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
package test;
成都创新互联公司主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务云南,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends Applet {
private final Panel pan = new Panel();
private final Label time = new Label();
private final Button btnGo = new Button("开始");
private final Button btnPouse = new Button("暂停");
private final Button btnReset = new Button("复位");
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.go();
btnGo.setEnabled(false);
}
}
private class btnPouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.setLabel("暂停");
}
}
private class StopwatchThread extends Thread {
private boolean going = false;
private long prevElapsed = 0;
private Date startDate = new Date();
private long elapsedTime() {
return prevElapsed +
(going ? new Date().getTime() - startDate.getTime() : 0);
}
private String msToString(long time) {
System.out.println(time+" "+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if (time % 1000 990)
time += 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
return min + ":" + sec + "." + ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
pan.add(new Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new Label());
add(pan);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnPouse.addActionListener(new btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改变有注释的那个if语句里面的值来判断什么时候停止
你可以在开始和结束的时候,分别记录下当前的时间的这毫秒数。然后再减,以下是一段代码。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
System.out.println("开始 "+startMili);
// 执行一段代码,求一百万次随机值
for(int i=0;i1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("结束 s"+endMili);
System.out.println("总耗时为:"+(endMili-startMili)+"毫秒");
}
}
第一种是以毫秒为单位计算的。
[java] view plain copy
//伪代码
long startTime=System.currentTimeMillis(); //获取开始时间
doSomeThing(); //测试的代码段
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(end-start)+"ms");
第二种是以纳秒为单位计算的。
[java] view plain copy
//伪代码
long startTime=System.nanoTime(); //获取开始时间
doSomeThing(); //测试的代码段
long endTime=System.nanoTime(); //获取结束时间
System.out.println("程序运行时间: "+(end-start)+"ns");
包含所需的包: import java.lang.System;