2016-12-06 117 views
-2

我正在开发这个小应用程序。在JFrame中,我有3个JSpinners来选择小时,分钟和秒。另外还有一个JButton开始时间。当我按它时它应该倒计时我选择使用JSpinners。有一个JLabel显示CountDown时间。我在StackOverFlow中搜索了很多帖子,但没有帮助我。这是我在帖子中找到的代码,根本不工作。任何人都可以请帮助?如何制作倒计时器Java

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); 
     } 
    } 

} 

enter image description here

+0

谁能帮帮我吗??? –

+0

也许你可以使用https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#scheduleAtFixedRate(java.util.TimerTask,%20long,%20long) –

回答

0

有很多错误的实现明智的。

没有必要扔在功能的异常

throws InterruptedException 

要创建您必须提供代码的新Thread要在运行该线程(又名Runnable):

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

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

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

你想通过倒计时来达到什么目的?什么时间和分钟?

我将提供一个解决方案,您将不得不完成代码才能正常工作,因为它会倒数整秒的总和。如果您想更新小时和分钟值,则必须自行处理。

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

希望你觉得它有用。随意对评论提问。

+0

谢谢你这么多@UDKOX 我会让你知道如果我有你的代码furthur问题 –

+0

在这里,你已经花时间(小时,分钟和秒)秒。如果它打印秒作为System.out.println(i);在示例中,它将只打印从零开始的秒。如附图所示,我所需要的只是当按下开始键时,秒数应该减少到零,直到分钟和小时变为零,就像秒表一样。任何想法我该怎么做? –

+0

@CodeBae我修复了你的代码并为你添加了解释。您只需更改* for *循环即可获得所需的输出。如果你不能自己做,也许你应该先阅读一些关于编程的书。如果您只是要复制/粘贴答案,那么做这件事毫无用处。 – UDKOX

0

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

您是说上面的代码无法正常工作。什么不工作?什么是症状,你期望发生什么?

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

首先,for循环应该开始:

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

其次,Thread thread = new Thread();完全不使用。

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

最后,您的条件是a <= 0而不是a >= 0

所以,你可以调整上述代码的方法之一是:

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); 
     } 
    } 
} 

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

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

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... 
    } 
} 

然后你的方法应该是这样的:

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

为什么不能使用Swing Timer而不使用线程? –