标签:时间 extend event static ram abs catch gets 进入
Thread sleep()休眠就是让线程进入休眠状态TIMED_WAITING,sleep("毫秒数"),当休眠时间到了之后继续线程。当然也可以用中断线程interrupt()来中断线程休眠。判断线程是否休眠if(ts.getstate()==state.TIMED_WAITING)
利用线程休眠来做秒表
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Time extends JFrame{
public Time(){
setTitle("秒表");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label=new JLabel("00:000");
Font f=new Font("微软雅黑",Font.BOLD,56);
label.setFont(f);
label.setBounds(30,10,250,100);
add(label);
JButton bt=new JButton("开始");
bt.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
long s=0,ms,start=System.currentTimeMillis();//获取系统当前时间
while (true){
ms=System.currentTimeMillis()-start;
s=ms/1000;ms=ms%1000;
String ss=s+"",sms=ms+"";
if (s<10) ss="0"+ss;
if (ms<10) sms="00"+ms;
else if (ms<100) sms="0"+ms;
String str=ss+":"+sms;
label.setText(str);
// System.out.println(str);
try {
Thread.sleep(10);//休眠10毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
bt.setBounds(30,200,80,30);
add(bt);
JButton bt1=new JButton("暂停");
bt1.setBounds(150,200,80,30);
add(bt1);
JLabel b=new JLabel();
add(b);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Time();
}
});
}
}
标签:时间 extend event static ram abs catch gets 进入
原文地址:http://www.cnblogs.com/feipengting/p/7542382.html