Java倒计时器

时间:2014-07-02 09:27:37

标签: java timer

您好我正在尝试为我的健身房制作一个倒计时秒表计时器。

以下是我实际显示和减少时间的内容:(请记住,我使用了我的正常秒表,只是简单地编辑了它以减少。它仍然是第一次通过,我仍然希望在某个阶段,但现在这应该有效。)

            new Timer(1,new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int seconds = (int)(System.currentTimeMillis()-watchStart)/1000;
                int days = seconds / 86400;
   //these are just initialization values for testing
                            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);
            }
    });

现在我的问题是:

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

}

1 个答案:

答案 0 :(得分:1)

使用标准库,特别是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应该能够提供有关正在发生的事情的见解。