简体   繁体   English

使用SwingWorker的GUI的倒数计时器-无法正确更新

[英]Countdown timer with GUI using SwingWorker - not updating properly

i'm trying to make a countdown timer using swingworker, problem is when seconds get to zero, programs sets them back to 60 and deducts one.I did make the program without GUI and it works just perfect, but using swingworker my timer looks like this 我正在尝试使用swingworker制作一个倒数计时器,问题是当秒数变为零时,程序将其设置回60并减去1,我确实使程序没有GUI且工作正常,但是使用swingworker我的计时器看起来像这个

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

which is kinda wrong and should be 这是一个错误,应该是
1: 0: 1 1:0:1
1: 0: 0 1:0:0
0: 59: 59 0:59:59

Both of my class lie on the same logic.I guess it has something to do with sending the whole Time object to the 'process' but i just can't explain it to myself. 我的两个类都基于相同的逻辑,我想这与将整个Time对象发送到``进程''有关,但我只是无法向自己解释。

doInBackground() method : 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;
        }

And this is the simple java class that works on the same logic : 这是在相同逻辑上工作的简单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;
                    }
                } 
            }
        }

If anyone could point me my mistake, i'd be very grateful. 如果有人能指出我的错误,我将不胜感激。 Thanks 谢谢

Adding process method 添加处理方法

public void process(List<Time> time) {
            for(Time t : time) {                    
                showField.setText(String.valueOf(t.getHours()) + ": " + String.valueOf(t.getMinutes())+ ": " + String.valueOf(t.getSeconds()));
            }
        }

I did not dig at your logic, but you can achieve the same using Calendar which looks more simple. 我没有研究您的逻辑,但是您可以使用看起来更简单的Calendar来实现相同的目的。

    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
    }

I have used SimpleDateFormat("HH:mm:ss") for simplicity. 为了简单起见,我使用了SimpleDateFormat("HH:mm:ss") If you need different fields of time, then you can use Calendar.get(..) method to get the same 如果需要不同的时间字段,则可以使用Calendar.get(..)方法获取相同的时间

your line 你的线

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

needs to be 需要是

if(time.getSeconds() == 0) time.setSeconds(59);

And invert this tow lines: 并反转这两条线:

time.setSeconds(time.getSeconds() - 1);
publish(time);

like this: 像这样:

publish(time);
time.setSeconds(time.getSeconds() - 1);

The deduction is correct but it's not showing the result because when seconds reaches cero the conditional it's executed, not showing the intermediate time. 推论是正确的,但是没有显示结果,因为当秒数达到条件时,它会执行,而不显示中间时间。

Change this line: 更改此行:

 if(time.getSeconds() == 0)

for this one: 为此:

 if(time.getSeconds() < 0)

And this: 和这个:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM