public class Daojishi extends Thread{
public static void main(String[] args) throws Exception{
for(int i=10;i>0;i--){
Thread.sleep(1000);
System.out.println("倒计时:"+i);
}
}
}
❷ 如何用java实现一个计时器
怎么还没人回答,看不过去了,用不用多线程根据你的程序需要,
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);//在1秒后执行此任务,每次间隔秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
while(true){//这个是用来停止此任务的,否则就一直循环执行此任务了
try {
int ch = System.in.read();
if(ch-'c'==0){
timer.cancel();//使用这个方法退出任务
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要进行的操作
}
}
}
大概就是这样了,在根据你的业务需要查一下资料,就可以搞定了!
❸ JAVA 计时器 求大神
//声明图形界面元素
privateJLabellab_time;
privateJButtonbut_start;
privateJButtonbut_end;
privateJButtonbut_reset;
//初始化界面元素,布局,注册时间监听器
setLayout(null);
setSize(400,300);
lab_time=newJLabel("");
lab_time.setBounds(0,10,100,50);
but_start=newJButton("开始");
but_start.setBounds(100,10,100,50);
but_end=newJButton("停止");
but_end.setBounds(200,10,100,50);
but_reset=newJButton("重置");
but_reset.setBounds(300,10,100,50);
myThread=newThread();
setTitle("计时器");
add(lab_time);
add(but_start);
add(but_end);
add(but_reset);
but_start.addActionListener(this);
but_end.addActionListener(this);
but_reset.addActionListener(this);
//事件处理方法
if(event.getSource()==but_start)
{
myThread.start();
}
if(event.getSource()==but_end)
{
myThread.stop();
}
if(event.getSource()==but_reset)
{
hour=0;
min=0;
sec=0;
this.showTime();
}
//线程处理代码
while(true)
{
try{
Thread.sleep(1000);
sec++;
if(sec==60)
{
min++;
sec=0;
}
if(min==60)
{
hour++;
min=0;
}
this.showTime();
}catch(Exceptione){
e.printStackTrace();
}
}
//设置窗体上显示时间
lab_time.setText(strTime);
好不容易打出来的,不知道你考试结束了嘛、希望能采纳吧!
❹ 用java编写一个计数器或计时器
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);//在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
while(true){//这个是用来停止此任务的,否则就一直循环执行此任务了
try {
int ch = System.in.read();
if(ch-'c'==0){
timer.cancel();//使用这个方法退出任务
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要进行的操作
}
}
}