Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java倒计时程序_Java_Timer - Fatal编程技术网

Java倒计时程序

Java倒计时程序,java,timer,Java,Timer,嗨,我正在尝试为我的健身房创建一个倒计时秒表计时器 以下是我对时间的实际显示和递减的了解:(请记住,我使用了我的普通秒表,只是简单地将其编辑为递减。这仍然是第一次通过,我仍然希望在某个阶段将其全部线程化,但目前这应该可以工作。) 现在我的问题是: a) 它不是递减开始的分钟数(如果它开始于5分钟) b) 如果我在30秒开始,它会变成负数,直到它达到一整分钟,然后递减。(我想我需要像if==0这样的方法,然后是分钟-1?但我担心这会破坏实际计数?) 谢谢 编辑:完整程序: package

嗨,我正在尝试为我的健身房创建一个倒计时秒表计时器

以下是我对时间的实际显示和递减的了解:(请记住,我使用了我的普通秒表,只是简单地将其编辑为递减。这仍然是第一次通过,我仍然希望在某个阶段将其全部线程化,但目前这应该可以工作。)

现在我的问题是:

a) 它不是递减开始的分钟数(如果它开始于5分钟)

b) 如果我在30秒开始,它会变成负数,直到它达到一整分钟,然后递减。(我想我需要像if==0这样的方法,然后是分钟-1?但我担心这会破坏实际计数?)

谢谢

编辑:完整程序:

    package countdown;

    import java.awt.*;
    import static java.awt.Frame.MAXIMIZED_BOTH;
    import javax.swing.Timer;
    import javax.swing.*;
    import java.awt.event.*;


    public class CountDown extends JFrame implements ActionListener{


    private int hour;
    private int minute;
    private int second;
// The component that shows the elapsed time.
private JLabel displayTimeLabel;


private long watchStart, watchEnd;


private Timer theChronometer;

// Keeps track of time when pausing the Timer.
private long pausedTime;

// Lets the program know if starting or resuming
private boolean paused = false;

// Button that changes from "Start" to "Resume" depending on pause status.
private JButton activateTimerButton;

// run the program
public static void main(String[] args) {

    CountDown count = new CountDown();
    count.setVisible(true);
    count.setLocationRelativeTo(null);
}


public CountDown(){

    // initialize
    super();
    setExtendedState(MAXIMIZED_BOTH);
    setLayout(new BorderLayout());


    setLayout(new GridLayout(2,1)); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("CrossFit Slam");
    setBackground(Color.black);
    setForeground(Color.white);



    Font largeFontBOLD = new Font("Calibri", Font.BOLD,20);
    Font largeFontPLAIN = new Font("Calibri", Font.PLAIN,200);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());

    activateTimerButton = new JButton("Start");// will display resume when the watch is paused
    JButton stopTimerButton = new JButton("Stop");
    JButton pauseTimerButton = new JButton("Pause");

    // register buttons to generate events when clicked
    activateTimerButton.addActionListener(this);
    stopTimerButton.addActionListener(this);
    pauseTimerButton.addActionListener(this);

    // the display for elapsed time
    displayTimeLabel = new JLabel("00:00:00");
    displayTimeLabel.setHorizontalAlignment(JLabel.CENTER);
    buttonPanel.setBackground(Color.black);
    buttonPanel.setForeground(Color.white);

    displayTimeLabel.setFont(largeFontPLAIN);
    displayTimeLabel.setForeground(Color.white);

    displayTimeLabel.setBackground(Color.black);

    activateTimerButton.setFont(largeFontBOLD);
    stopTimerButton.setFont(largeFontBOLD);
    pauseTimerButton.setFont(largeFontBOLD);

displayTimeLabel.setOpaque(true);


    buttonPanel.add(activateTimerButton);
    buttonPanel.add(stopTimerButton);
    buttonPanel.add(pauseTimerButton);

    add(displayTimeLabel);
    add(buttonPanel, BorderLayout.PAGE_END);


    theChronometer =
    new Timer(1,new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int seconds = (int)(System.currentTimeMillis()-watchStart)/1000;
                int days = seconds / 86400;
                            int startHour, startMin, startSec;
                            startHour = 5;
                            startMin = 5;
                            startSec = 0;
                int hours = (seconds / 3600) - (days * 24);
                int min = (seconds / 60) - (days * 1440) - (hours * 60);
                int sec = seconds % 60;
                                    String s = String.format("%02d:%02d:%02d", startHour - hours, startMin - min, ((startSec == 0) ? startSec = 60 : startSec) - sec );

                displayTimeLabel.setText(s);
            }
    });
}



public void actionPerformed(ActionEvent e){


    if(e.getActionCommand().equals("Stop")){theChronometer.stop();}


    else if(e.getActionCommand().equals("Start") || e.getActionCommand().equals("Resume")){
        if(!paused){
           watchStart = System.currentTimeMillis();
           theChronometer.start();
        }
         else{
            watchStart = System.currentTimeMillis()+pausedTime;
            pausedTime = 0;
            theChronometer.start();
            paused = false;
            activateTimerButton.setText("Start");
         }
    }


    else if(e.getActionCommand().equals("Pause")){
        long now = System.currentTimeMillis();
        pausedTime -= (now - watchStart);
        theChronometer.stop();
        paused = true;
        activateTimerButton.setText("Resume");
    }
}

}

使用标准库,特别是
Date
类,可以轻松实现您的目标

DateFormat f = new SimpleDateFormat("HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
long startingTime = TimeUnit.HOURS.toMillis(5) + TimeUnit.MINUTES.toMillis(5);

public void actionPerformed(ActionEvent e) {
    long elapsed = System.currentTimeMillis() - startTimestamp;
    long displayTs = startingTime - elapsed;

    String out;
    if (displayTs >= 0) {
        out = f.format(new Date(displayTs));
    } else {
        out = "-" + f.format(new Date(-displayTs));
    }
    displayTimeLabel.setText(out);
}

所有这些都在标准Java库中,方法/类上的Javadoc应该能够提供关于发生了什么的见解。

请发布一个。抱歉,现在就可以了。当1秒过去时,你的
min
为0,那么为什么你会期望它是5以外的值呢?你需要在那里做一些巧妙的取整。或者使用库方法。即使是标准的Java
Date
也可以毫无错误地完成所有这些,更不用说更好的了……这段代码的目的是什么?学习如何使用时间戳或制作简单的秒表?在后一种情况下,我可以发布一个问题的三行解决方案。我很乐意看到这一点。我以前从未使用过计时器或日期类。(可能使用了日期,但只是为了显示当前日期)也希望看到其他方法,并看看如何将其合并到那里。我一直在谷歌上搜索,但我发现没有任何东西适合我希望它的操作方式。谢谢,我会尝试一下,如果我有任何问题,会告诉你。哎呀,忘记时区了,秒。@user2792920小时字段可能有问题,因为格式将在你的时区,而不是GMT(忘记这是我的默认设置,因此对我有效)。我添加了设置时区的功能,现在应该可以了。我正在尝试锻炼你的“startTimestamp”来自哪里?您是用当前时间初始化它还是指其他内容?@user2792920这是您的watchStart。你可以想象,这是倒计时开始的时间戳。
DateFormat f = new SimpleDateFormat("HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
long startingTime = TimeUnit.HOURS.toMillis(5) + TimeUnit.MINUTES.toMillis(5);

public void actionPerformed(ActionEvent e) {
    long elapsed = System.currentTimeMillis() - startTimestamp;
    long displayTs = startingTime - elapsed;

    String out;
    if (displayTs >= 0) {
        out = f.format(new Date(displayTs));
    } else {
        out = "-" + f.format(new Date(-displayTs));
    }
    displayTimeLabel.setText(out);
}