Java 使用SwingWorker的GUI倒计时-未正确更新

Java 使用SwingWorker的GUI倒计时-未正确更新,java,swingworker,Java,Swingworker,我正在尝试使用swingworker制作一个倒计时计时器,问题是当秒数变为零时,程序会将它们设置为60并扣除一个。我确实在没有GUI的情况下制作了这个程序,它工作得非常完美,但使用swingworker我的计时器看起来是这样的 1:0:2 1:0:1 0:59:60 这有点错误,应该是 1:0:1 1:0:0 0:59:59 我的两个类都有相同的逻辑。我想这与将整个时间对象发送到“进程”有关,但我无法向自己解释 doInBackground()方法: protected Void doInBac

我正在尝试使用swingworker制作一个倒计时计时器,问题是当秒数变为零时,程序会将它们设置为60并扣除一个。我确实在没有GUI的情况下制作了这个程序,它工作得非常完美,但使用swingworker我的计时器看起来是这样的

1:0:2
1:0:1
0:59:60

这有点错误,应该是
1:0:1
1:0:0
0:59:59

我的两个类都有相同的逻辑。我想这与将整个时间对象发送到“进程”有关,但我无法向自己解释

doInBackground()方法:

protected Void doInBackground() throws Exception {
            Integer hours = Integer.parseInt(hoursField.getText());
            Integer minutes = Integer.parseInt(minutesField.getText());
            Integer seconds = Integer.parseInt(secondsField.getText());

            Time time = new Time(hours, minutes, seconds);

            if(minutes < 59 & seconds < 59) {               
                if(hours >=0 & minutes >=0 & seconds >=0) {
                    boolean count = true;
                    while(count) {
                        try {
                            Thread.sleep(1000);
                        } catch(InterruptedException e) {
                            Logger.getLogger(Chronometer.class.getName()).log(Level.SEVERE, null, e);
                        }
                        time.setSeconds(time.getSeconds() - 1);
                        publish(time);
                        if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() == 0) {
                            count = false;
                        }
                        if(time.getSeconds() == 0) {
                            time.setSeconds(60);
                            if(time.getMinutes() != 0) {
                                time.setMinutes((time.getMinutes() - 1));
                            }else if(time.getMinutes() == 0) {
                                time.setHours((time.getHours() - 1));
                                if(time.getHours() >= 0) {
                                    time.setMinutes(59);
                                }                                    
                                if(time.getHours() < 0) {
                                    time.setHours(0);
                                }
                            }
                        }

                    }
                }else {
                    System.exit(0);
                }
            }
            return null;
        }
protectedvoid doInBackground()引发异常{
整数小时=Integer.parseInt(hoursField.getText());
整数分钟=Integer.parseInt(minutesField.getText());
整数秒=Integer.parseInt(secondsField.getText());
时间=新时间(小时、分钟、秒);
如果(分<59秒<59秒){
如果(小时>=0分钟>=0秒>=0){
布尔计数=真;
同时(计数){
试一试{
睡眠(1000);
}捕捉(中断异常e){
Logger.getLogger(Chronometer.class.getName()).log(Level.SEVERE,null,e);
}
time.setSeconds(time.getSeconds()-1);
出版(时间);
if(time.getHours()==0和time.getMinutes()==0和time.getSeconds()==0){
计数=假;
}
if(time.getSeconds()==0){
时间。设置秒(60);
if(time.getMinutes()!=0){
time.setMinutes((time.getMinutes()-1));
}else if(time.getMinutes()==0){
time.setHours((time.getHours()-1));
如果(time.getHours()>=0){
时间。设定分钟(59);
}                                    
if(time.getHours()<0){
时间。设定小时数(0);
}
}
}
}
}否则{
系统出口(0);
}
}
返回null;
}
这是一个简单的java类,在相同的逻辑上工作:

boolean count = true;
        while(count) {
            try {
                Thread.sleep(1000);
            } catch(InterruptedException e) {

            }
            seconds--;
            System.out.println(hours + ": " + minutes + ": " + seconds);
            if(hours == 0 & minutes == 0 & seconds == 0) {
                count = false;
            }
            if(seconds == 0) {
                seconds = 60;
                if(minutes != 0){ 
                    minutes--;
                }else if(minutes == 0) {
                    hours--;
                    if(hours >=0){
                        minutes = 59;
                    }                    
                    if(hours < 0) {
                        hours = 0;
                    }
                } 
            }
        }
boolean count=true;
同时(计数){
试一试{
睡眠(1000);
}捕捉(中断异常e){
}
秒--;
系统输出打印项次(小时+“:“+分钟+”:“+秒);
如果(小时=0分钟=0秒=0){
计数=假;
}
如果(秒==0){
秒=60;
如果(分钟!=0){
分钟--;
}否则如果(分钟==0){
小时--;
如果(小时数>=0){
分钟=59;
}                    
如果(小时<0){
小时=0;
}
} 
}
}
如果有人能指出我的错误,我将非常感激。 谢谢

添加过程方法

public void process(List<Time> time) {
            for(Time t : time) {                    
                showField.setText(String.valueOf(t.getHours()) + ": " + String.valueOf(t.getMinutes())+ ": " + String.valueOf(t.getSeconds()));
            }
        }
公共作废流程(列表时间){
对于(时间t:Time){
showField.setText(String.valueOf(t.getHours())+“:”+String.valueOf(t.getMinutes())+“:”+String.valueOf(t.getSeconds());
}
}

我没有深入研究您的逻辑,但是您可以使用看起来更简单的
日历来实现同样的功能

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hours);
    c.set(Calendar.MINUTE, minutes);
    c.set(Calendar.SECOND, seconds);
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

    Date d = c.getTime();
    System.out.println(format.format(d)); 
    while(count) {
        c.set(Calendar.SECOND, c.get(Calendar.SECOND)-1); //decrement 1 second at a time
        d = c.getTime();
        System.out.println(format.format(d)); 
        Thread.sleep(1000); //Pause for a second between each print
    }
为了简单起见,我使用了
SimpleDateFormat(“HH:mm:ss”)
。如果您需要不同的时间字段,那么您可以使用
Calendar.get(..)
方法来获取相同的行

if(time.getSeconds() == 0) time.setSeconds(60);
需要

if(time.getSeconds() == 0) time.setSeconds(59);
将这两条线颠倒过来:

time.setSeconds(time.getSeconds() - 1);
publish(time);
像这样:

publish(time);
time.setSeconds(time.getSeconds() - 1);
这个推断是正确的,但它没有显示结果,因为当秒数达到cero时,条件执行,没有显示中间时间

更改此行:

 if(time.getSeconds() == 0)
对于这一个:

 if(time.getSeconds() < 0)
if(time.getSeconds()<0)
这是:

 if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() == 0)

 if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() < 0)
if(time.getHours()==0和time.getMinutes()==0和time.getSeconds()==0)
if(time.getHours()==0和time.getMinutes()==0和time.getSeconds()<0)

我从一个表单中获取输入,在该表单中,您在文本字段中键入值,然后程序应该开始倒计时,直到它变为零,我没有发布所有程序。
小时
分钟
您的输入正确吗?。。如果您在第2,3,4行中看到,我使用的是sametime.setSeconds(60);应为时间。设置秒(59);1:0:2 1:0:1 0:59:59再次输出错误,这不是逻辑问题,程序在没有SwingWorker的情况下运行(只是控制台上的一个打印),没问题,我想这与将上课时间发送到进程和打印正确无误有关。@Radoslav添加到回答1 0 2 0 1 0 59仍然,好多了,但没有达到预期效果。。。我怀疑这是关于行的顺序。我在没有swingworker及其方法(在控制台上打印行)的情况下编写了相同的程序,它工作得很好。。。这就是让我困惑的地方。。。也许当发送对象处理时间并对整个列表进行评分时…@Radoslav添加到回答中最终起作用了,似乎不是SwingWorker,谢谢你的时间伙伴认为我离开了如果(time.getHours()==0&time.getMinutes()==0&time.getSeconds()==0),那么它可以在0:0:0完成,并且仍然有效,非常感谢