58,453
社区成员
发帖
与我相关
我的任务
分享
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//为什么不加睡眠的时候有错误呢?
//try {
// Thread.sleep(100);
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
package time;
//计时的类,有开始计时,暂停计时,停止计时等功能
//停止计时之后,计时线程将消亡
public class Time implements Runnable{
private int minute;//分钟数
private int second;//秒数
private long startMillis;//开始的豪秒数
private long newMillis;//新的毫秒数
private long totalMillis;//总的毫秒数秒数
private long pauseMillis;//暂停毫秒数
private Thread clockThread;//走时线程
private int countState = 2;//计数状态,1是正常计数,2是暂停,3是停止,初始设置为暂停状态
public Time(){
resetTime();//设置初始值0
clockThread = new Thread(this);//创建线程
clockThread.start();//使线程进入就绪状态
}
@Override
public void run() {
while(countState != 3){//还没停止,进入循环状态
if(countState == 1){//正常计数状态
//定时更新时间
newMillis = System.currentTimeMillis();//现在的时间
totalMillis = newMillis - startMillis;//总的毫秒数
System.out.println(totalMillis);
second = (int) (totalMillis / 1000) % 60;
minute = (int) (totalMillis / 1000 / 60);
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//一旦进入停止状态,线程结束
}
public void pauseCount(){
countState = 2;//暂停计数
pauseMillis = totalMillis;//记录暂停时的总的毫秒数
}
public void startCount(){
countState = 1;//开始计数
startMillis = System.currentTimeMillis() - pauseMillis;//读取虚拟的开始时刻
}
public void stopCount(){
countState = 3;//停止计数,此时线程即将死亡
}
public void resetTime(){
minute = 0;
second = 0;
startMillis = 0;
pauseMillis = 0;
totalMillis = 0;
newMillis = 0;
}
public int getMinute(){
return minute;
}
public int getSecond(){
return second;
}
public long getTotal(){
return totalMillis;
}
}
package test;
//还是有问题,我想实现停止计时之后还是使用同个Time对象
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import time.Time;
public class TimeTest extends JFrame{
private JLabel timeText,totalText;
private JButton startButton;
private JButton pauseButton;
private JButton endButton;
private JButton totalButton;
private Time time = new Time();
public TimeTest(){
super("秒表功能演示");
Container container = getContentPane();
container.setLayout(new FlowLayout());//设置布局
timeText = new JLabel("时间:00M:00S");
totalText = new JLabel("总的毫秒数是:0" );
container.add(timeText);
container.add(totalText);
startButton = new JButton("开始计时");
pauseButton = new JButton("暂停计时");
endButton = new JButton("停止计时");
totalButton = new JButton("总的毫秒数");
container.add(startButton);
container.add(pauseButton);
container.add(endButton);
container.add(totalButton);
Listener listener = new Listener();
startButton.addActionListener(listener);
pauseButton.addActionListener(listener);
endButton.addActionListener(listener);
totalButton.addActionListener(listener);
setSize(250, 150);
setVisible(true);
}
public static void main(String[] args) {
TimeTest test = new TimeTest();
}
private class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == startButton){
time.startCount();
System.out.println("startcount");
}
if(e.getSource() == pauseButton){
time.pauseCount();
}
if(e.getSource() == endButton){
timeText.setText("时间:" + time.getMinute() + "M:" + time.getSecond() + "S");
time.stopCount();
time = null;
time = new Time();
}
if(e.getSource() == totalButton){
totalText.setText("总的毫秒数是:" + time.getTotal());
}
}
}
}