Java 中的 1 周倒数计时器

问题描述 投票:0回答:3

编辑: 我已经创建了一个倒数计时器,但是我不知道如何在不创建新实例的情况下进行更新。代码如下:

import java.util.concurrent.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.DAYS;

public class Countdown {
static int countdownStarterD = 7;
static int countdownStarterH = 24;
static int countdownStarterM = 60;
static int countdownStarterS = 60;
String cSD = Integer.toString(countdownStarterD);
String cSH = Integer.toString(countdownStarterH);
String cSM = Integer.toString(countdownStarterM);
String cSS = Integer.toString(countdownStarterS);
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel days = new JLabel(cSD);
JLabel hours = new JLabel(cSH);
JLabel minutes = new JLabel(cSM);
JLabel seconds = new JLabel(cSS);

public Countdown() {
    panel.add(days);
    panel.add(hours);
    panel.add(minutes);
    panel.add(seconds);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Countdown Timer");
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    final Runnable days = new Runnable() {

        public void run() {

            System.out.println(countdownStarterD);
            countdownStarterD--;

            if (countdownStarterD < 0) {
                countdownStarterD = 6;
            }
        }
    };
    final Runnable hours = new Runnable() {

        public void run() {

            System.out.println(countdownStarterH);
            countdownStarterH--;

            if (countdownStarterH < 0) {
                countdownStarterH = 23;
            }
        }
    };
    final Runnable minutes = new Runnable() {

        public void run() {

            System.out.println(countdownStarterM);
            countdownStarterM--;

            if (countdownStarterM < 0) {
                countdownStarterM = 59;
            }
        }
    };
    final Runnable seconds = new Runnable() {

        public void run() {

            System.out.println(countdownStarterS);
            countdownStarterS--;

            if (countdownStarterS < 0) {
                countdownStarterS = 59;
            }
        }
    };
    final Runnable gui = new Runnable() {

        public void run() {
            new Countdown();
            
        }
    };
    scheduler.scheduleAtFixedRate(days, 0, 1, DAYS);
    scheduler.scheduleAtFixedRate(hours, 0, 1, HOURS);
    scheduler.scheduleAtFixedRate(minutes, 0, 1, MINUTES);
    scheduler.scheduleAtFixedRate(seconds, 0, 1, SECONDS);
    scheduler.scheduleAtFixedRate(gui, 0, 1, SECONDS);
    
}

}

旧: 我正在尝试创建一个简单的 GUI 计时器,它从 1 周开始倒计时,并在完成后重新启动,它不需要运行任何东西,只需显示倒计时。我也想要它所以有一个固定的时间。我一直在尝试将倒计时中的秒数转换为天数:小时数:分钟数:秒数,但我不知道如何将该转换连接到 GUI。我想我可能会使用 JLabel,但我不确定。

到目前为止,我在另一个论坛上找到了这段代码。

import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.SECONDS;

public class Countdown {
    public static void main(String[] args) {

        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        final Runnable runnable = new Runnable() {
            int countdownStarter = 20;

            public void run() {

                System.out.println(countdownStarter);
                countdownStarter--;

                if (countdownStarter < 0) {
                    System.out.println("Timer Over!");
                    scheduler.shutdown();
                }
            }
        };
        scheduler.scheduleAtFixedRate(runnable, 0, 1, SECONDS);
    }
}

现在从20开始倒计时,可以通过改变来改变

int countdownStarter = 20;

我注意到有一个

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

可以在我目前的代码中使用。对此的任何帮助将不胜感激。我不是在寻找替代应用程序来代替。

java windows countdown
3个回答
0
投票

正如您已经注意到的,您可以使用 TimeUnit 类来执行转换。这是一个例子:

int seconds = 3600;
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
System.out.println(day + ":" + hours + ":" + minute + ":" + second);

关于输出的可能性,这取决于你想如何显示它。 JFrame、Terminal 等等。


0
投票

java.time

你似乎在这里工作太辛苦了。我发现您的代码难以遵循,并且可能过度劳累。 Java 在 JSR 310 中定义的 java.time 类中具有行业领先的日期时间框架。利用它。

使用
SwingTimer
,而不是你自己的线程

正如其他人评论的那样,对于 Swing 应用程序,您应该使用

SwingTimer
类,而不是在您自己的线程和执行程序服务中倒计时。

跟踪时间过去

关于确定特定时刻之前剩余的天数、小时数、分钟数、秒数和纳秒的具体问题:

  • 决定是否要跟踪您的时间跨度 (a) 按日历日或 (b) 按通用 24 小时分块“天”。
  • 存储您的目标时刻。
  • 捕捉当前时刻。
  • 计算经过的时间。

我会假设你想要 24 小时分块天而不是日历天。

Instant
类代表一个时刻,时间轴上的一个特定点,如 UTC 中所示(与 UTC 的零小时-分钟-秒的偏移量)。

Duration
类代表一个时间跨度,不附加到时间轴,在 24 小时通用天、小时、分钟、秒和纳米的尺度上。

Duration week = Duration.ofDays( 7 ) ;
Instant target = Instant.now().plus( week ) ;

当您的计时器任务运行时,捕获当前时刻,并计算剩余时间。

Duration timeRemaining = Duration.between( Instant.now() , target ) ;

检查剩余持续时间是否为正是否为负

  • 如果为正,则目标仍在未来。所以报告它的当前值。
  • 如果为负,则目标已通过。于是重新计算下一个目标,下周出来。并报告新值。

-2
投票

请花时间查看此 API:https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

这应该为您提供所需的所有信息。

也因为你提到了JLabel,我猜你想使用和学习: https://www.javatpoint.com/java-swing

© www.soinside.com 2019 - 2024. All rights reserved.