当前位置: 代码迷 >> J2SE >> Java 中怎么实现倒计时
  详细解决方案

Java 中怎么实现倒计时

热度:9943   发布时间:2013-02-25 00:00:00.0
Java 中如何实现倒计时?
比如,一个任务完成需要10分钟,让倒计时按秒来倒计时,怎么做?

------解决方案--------------------------------------------------------
探讨
用java.util.Timer可以实现的。控制在10分钟然后处理你要的倒计时。

------解决方案--------------------------------------------------------
Timer就可以啊。
我刚刚写了一个类似的代码段,不知道是不是lz所需要的。
Java code
        final Action taskPerformer = new AbstractAction() {            private static final long serialVersionUID = 1L;            public void actionPerformed(ActionEvent evt) {                Display.getDefault().syncExec(new Runnable() {                    public void run() {                        // TODO        };        timer = new Timer(1000, taskPerformer);        timer.start();
------解决方案--------------------------------------------------------
非timer实现的倒计时
Java code
public class 倒计时时钟 {    //小时    private int hours;        //分钟    private int min;        //秒    private int second;        public 倒计时时钟(int hours,int min,int second) {        this.hours = hours;        this.min = min;        this.second = second;    }    public int getHours() {        return hours;    }    public void setHours(int hours) {        this.hours = hours;    }    public int getMin() {        return min;    }    public void setMin(int min) {        this.min = min;    }    public int getSecond() {        return second;    }    public void setSecond(int second) {        this.second = second;    }}public class 倒计时 implements Runnable{    private 倒计时时钟 clock;    private long time;        public 倒计时(倒计时时钟 clock) {        this.clock = clock;        //将时间换算成秒        time = clock.getHours()*60*60+clock.getMin()*60+clock.getSecond();    }    public void run() {        while(time >= 0) {            try {                Thread.sleep(1000);                time -= 1;//时间减去一秒                clock.setHours((int)time/(60*60));                clock.setMin((int)(time/60)%60);                clock.setSecond((int)time % 60);                            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        //return clock;    }        public 倒计时时钟 getTime() {        return clock;    }        public static void main(String [] args) {        倒计时时钟 clock = new 倒计时时钟(0,10,0);                倒计时 jishi = new 倒计时(clock);        显示 show = new 显示(jishi.getTime());        //显示 show = new 显示();        new Thread(show).start();        new Thread(jishi).start();            }}    class 显示 implements Runnable {        private 倒计时时钟 clock;        public 显示(倒计时时钟 clock) {            this.clock = clock;                    }                public void run() {                        while(clock.getHours() != 0 ||                     clock.getMin() != 0 ||                     clock.getSecond() != 0) {                try {                    System.out.println(String.format("%02d",clock.getHours())+                            ":"+String.format("%02d",clock.getMin())+                            ":"+String.format("%02d",clock.getSecond()));                    Thread.sleep(1000);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }                    }    }
  相关解决方案