如何制作CountDown计时器Java

时间:2022-07-29 02:47:02

I was developing this small application. In a JFrame, I have 3 JSpinners to select Hours, Minutes and Seconds. In addition there is a JButton to Start the Time. When I press it It should Count Down the Time that I selected using JSpinners. There is a JLabel to show the CountDown time. I have searched many posts in * but nothing Helped me. This is a code I found in a Post and It is not working at all. Can anyone please Help???

我正在开发这个小应用程序。在JFrame中,我有3个JSpinner来选择小时,分钟和秒。此外还有一个JButton来开始时间。当我按下它时应该倒数我使用JSpinners选择的时间。有一个JLabel显示CountDown时间。我在*中搜索了很多帖子,但没有帮助我。这是我在帖子中找到的代码,它根本不起作用。谁能请帮忙???

private void countDownTimer() throws InterruptedException {
    Thread thread = new Thread();
    int hours = (int) jSpinner1.getValue();
    int minutes = (int) jSpinner2.getValue();
    int seconds = (int) jSpinner3.getValue();

    for (int a = seconds; a <= 0; a--) {

            Thread.sleep(1000);
            System.out.println(a);
        }
    }

}

如何制作CountDown计时器Java

2 个解决方案

#1


1  

There are many mistakes implementation wise.

实施明智有很多错误。

There is no need to throw an Exception on the function

无需在函数上抛出异常

throws InterruptedException 

To create a new Thread you have to provide the code to be run in said thread (AKA Runnable):

要创建新线程,您必须提供要在所述线程中运行的代码(AKA Runnable):

Thread thread = new Thread(new Runnable () {
        @Override
        public void run(){
            // Your code here
        }
    });
thread.start();

Apart from that, I cannot understand the goal of the following code:

除此之外,我无法理解以下代码的目标:

for (int a = seconds; a <= 0; a--) {
    Thread.sleep(1000);
    System.out.println(a);
}

What are you trying to achieve by counting down the seconds? What about the hours and minutes?

你想通过倒计时秒来实现什么目标?小时和分钟怎么样?

I will provide a solution that you will have to complete for the code to work as you want as it will countdown the whole seconds sum. If you want to update the hour and minute values you will have to work on that yourself.

我将提供一个解决方案,您必须完成该代码才能按照您想要的方式工作,因为它将倒数整个秒数。如果您想更新小时和分钟值,您必须自己处理。

private void countDownTimer() {

    final int hours = (int) jSpinner1.getValue();
    final int minutes = (int) jSpinner2.getValue();
    final int seconds = (int) jSpinner3.getValue();

    // Create Thread not to block UI
    Thread thread = new Thread(new Runnable () {

        @Override
        public void run() {
            // Calculate total seconds to count down
            int countdownSeconds = hours * 3600 + minutes * 60 + seconds;

            // Count down to 0 and print it on the console
            for (int i = countdownSeconds ; i >= 0; i--) {

                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e) {}

                System.out.println(i);
            }
        }
    });
    // Start the Thread
    thread.start();
}

Hope you find it usefull. Feel free to ask questions on the comments.

希望你发现它很有用。随意提出评论问题。

#2


0  

I can't comment on your question because I don't have 50 points, so I will have to write an answer with a question here.

我不能评论你的问题,因为我没有50分,所以我必须在这里写一个问题的答案。

You are saying that the code above is not working. What exactly is not working? What are the symptoms and what do you expect to happen?

你是说上面的代码不起作用。究竟什么不起作用?有什么症状,你期望发生什么?

One of the problems is that you are iterating over seconds only, ignoring minutes and hours completely. The other one is that you are creating a thread that is not used at all. The third one is that you are invoking Thread.sleep(1000) inside the for loop, but I am not sure if you are aware that it would block the Thread completely until it's finished.

其中一个问题是你只是在几秒钟内迭代,完全忽略了几分钟和几小时。另一个是您正在创建一个根本不使用的线程。第三个是你在for循环中调用Thread.sleep(1000),但是我不确定你是否知道它会完全阻塞线程直到它完成。

Firstly, the for loop should start with:

首先,for循环应该从以下开始:

for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--)

Secondly, Thread thread = new Thread(); is not used at all.

其次,Thread thread = new Thread();根本没用过。

Thirdy, Thread.sleep(1000) will block the current thread until it's finished.

第三,Thread.sleep(1000)将阻塞当前线程,直到它完成。

And lastly, your condition is saying a <= 0 instead of a >= 0.

最后,你的条件是说<= 0而不是> = 0。

So one of the ways you can adjust the above code is:

因此,您可以调整上述代码的方法之一是:

private void countDownTimer() throws InterruptedException {
    int hours = (int) jSpinner1.getValue();
    int minutes = (int) jSpinner2.getValue();
    int seconds = (int) jSpinner3.getValue();

    for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--) {
            Thread.sleep(1000);
            System.out.println(a);
        }
    }
}

This is assuming that the 'not working' means it's ignoring minutes and hours.

这假设“不工作”意味着它忽略了分钟和小时。

The other solution is to create a new thread with a callback function, that will be invoked once the countdown timer reaches zero:

另一种解决方案是创建一个带回调函数的新线程,一旦倒数计时器到达零,将调用该线程:

public class CountDownRunnable implements Runnable {
    private final int hours;
    private final int minutes;
    private final int seconds;

    public CountDownRunnable(int hours, int minutes, int seconds) {
      this.hours = hours;
      this.minutes = minutes;
      this.seconds = seconds;
    }

    public void run() {
        for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--) {
            Thread.sleep(1000);
            System.out.println(a);
        }

        // Here goes your code that will be invoked once the timer reaches zero...
    }
}

Then your method would look like this:

然后你的方法看起来像这样:

private void countDownTimer() throws InterruptedException {
    (new Thread(new HelloRunnable())).start();
}

#1


1  

There are many mistakes implementation wise.

实施明智有很多错误。

There is no need to throw an Exception on the function

无需在函数上抛出异常

throws InterruptedException 

To create a new Thread you have to provide the code to be run in said thread (AKA Runnable):

要创建新线程,您必须提供要在所述线程中运行的代码(AKA Runnable):

Thread thread = new Thread(new Runnable () {
        @Override
        public void run(){
            // Your code here
        }
    });
thread.start();

Apart from that, I cannot understand the goal of the following code:

除此之外,我无法理解以下代码的目标:

for (int a = seconds; a <= 0; a--) {
    Thread.sleep(1000);
    System.out.println(a);
}

What are you trying to achieve by counting down the seconds? What about the hours and minutes?

你想通过倒计时秒来实现什么目标?小时和分钟怎么样?

I will provide a solution that you will have to complete for the code to work as you want as it will countdown the whole seconds sum. If you want to update the hour and minute values you will have to work on that yourself.

我将提供一个解决方案,您必须完成该代码才能按照您想要的方式工作,因为它将倒数整个秒数。如果您想更新小时和分钟值,您必须自己处理。

private void countDownTimer() {

    final int hours = (int) jSpinner1.getValue();
    final int minutes = (int) jSpinner2.getValue();
    final int seconds = (int) jSpinner3.getValue();

    // Create Thread not to block UI
    Thread thread = new Thread(new Runnable () {

        @Override
        public void run() {
            // Calculate total seconds to count down
            int countdownSeconds = hours * 3600 + minutes * 60 + seconds;

            // Count down to 0 and print it on the console
            for (int i = countdownSeconds ; i >= 0; i--) {

                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e) {}

                System.out.println(i);
            }
        }
    });
    // Start the Thread
    thread.start();
}

Hope you find it usefull. Feel free to ask questions on the comments.

希望你发现它很有用。随意提出评论问题。

#2


0  

I can't comment on your question because I don't have 50 points, so I will have to write an answer with a question here.

我不能评论你的问题,因为我没有50分,所以我必须在这里写一个问题的答案。

You are saying that the code above is not working. What exactly is not working? What are the symptoms and what do you expect to happen?

你是说上面的代码不起作用。究竟什么不起作用?有什么症状,你期望发生什么?

One of the problems is that you are iterating over seconds only, ignoring minutes and hours completely. The other one is that you are creating a thread that is not used at all. The third one is that you are invoking Thread.sleep(1000) inside the for loop, but I am not sure if you are aware that it would block the Thread completely until it's finished.

其中一个问题是你只是在几秒钟内迭代,完全忽略了几分钟和几小时。另一个是您正在创建一个根本不使用的线程。第三个是你在for循环中调用Thread.sleep(1000),但是我不确定你是否知道它会完全阻塞线程直到它完成。

Firstly, the for loop should start with:

首先,for循环应该从以下开始:

for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--)

Secondly, Thread thread = new Thread(); is not used at all.

其次,Thread thread = new Thread();根本没用过。

Thirdy, Thread.sleep(1000) will block the current thread until it's finished.

第三,Thread.sleep(1000)将阻塞当前线程,直到它完成。

And lastly, your condition is saying a <= 0 instead of a >= 0.

最后,你的条件是说<= 0而不是> = 0。

So one of the ways you can adjust the above code is:

因此,您可以调整上述代码的方法之一是:

private void countDownTimer() throws InterruptedException {
    int hours = (int) jSpinner1.getValue();
    int minutes = (int) jSpinner2.getValue();
    int seconds = (int) jSpinner3.getValue();

    for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--) {
            Thread.sleep(1000);
            System.out.println(a);
        }
    }
}

This is assuming that the 'not working' means it's ignoring minutes and hours.

这假设“不工作”意味着它忽略了分钟和小时。

The other solution is to create a new thread with a callback function, that will be invoked once the countdown timer reaches zero:

另一种解决方案是创建一个带回调函数的新线程,一旦倒数计时器到达零,将调用该线程:

public class CountDownRunnable implements Runnable {
    private final int hours;
    private final int minutes;
    private final int seconds;

    public CountDownRunnable(int hours, int minutes, int seconds) {
      this.hours = hours;
      this.minutes = minutes;
      this.seconds = seconds;
    }

    public void run() {
        for (int a = hours * 3600 + 60 * minutes + seconds; a >= 0; a--) {
            Thread.sleep(1000);
            System.out.println(a);
        }

        // Here goes your code that will be invoked once the timer reaches zero...
    }
}

Then your method would look like this:

然后你的方法看起来像这样:

private void countDownTimer() throws InterruptedException {
    (new Thread(new HelloRunnable())).start();
}