简体   繁体   中英

Java swing countdown timer not working properly

I would like to make a program that shuts down the computer after specific time, but I have troubles making the timer countdown. It must decrement the time in the spinners from which it takes it. I tried to make a dynamic dialog application but it doesn't work.
I apply the whole code if that is not enough.

timer.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
           //decrements the amount of seconds and the time in the spinners
           m_secs--;
           ShutDownDialog.this.AmountOfTime--;
           if (m_secs<0) {
                m_secs=59;
                m_mins--;

                if (m_mins<0) {
                    m_mins=59;
                    m_hours--;

                    if(m_hours<0)
                        m_hours=0;
                }
                hours.setValue((Integer)m_hours);
                minutes.setValue((Integer)m_mins);
                seconds.setValue((Integer)m_secs);
                label_2.setText(ShutDownDialog.       
                this.AmountOfTime.toString());
            }
            if (ShutDownDialog.this.AmountOfTime< 0)
                {
                timer.stop();
                label_2.setText("stop"); //just to check what happens
                }
        }
    });

   StartCountdown.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            hours.setEnabled(false);
            minutes.setEnabled(false);
            seconds.setEnabled(false);
            StartCountdown.setEnabled(false);
            StopCount.setEnabled(true);
            //calculates the amount of seconds and starts the timer
            m_hours = ((Integer)hours.getValue()*3600); //get the hours
            m_mins = ((Integer)minutes.getValue()*60); //get the minutes
            m_secs = (Integer)seconds.getValue(); //get seconds
            AmountOfTime = m_hours + m_mins + m_secs;
            timer.start();
            //JOptionPane.showMessageDialog(null, AmountOfTime);
        }
    });

You are updating the values only, if m_secs < 0 .

Move

hours.setValue((Integer) m_hours);
minutes.setValue((Integer) m_mins);
seconds.setValue((Integer) m_secs);

outside of the if-block and it will update the labels. It should now look like this:

private void initialize() {
    timer.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            m_secs--;
            ShutDownDialog.this.AmountOfTime--;
            if (m_secs<0) {
                m_secs=59;
                m_mins--;

                if (m_mins<0) {
                    m_mins=59;
                    m_hours--;

                    if(m_hours<0)
                        m_hours=0;
                }
            }
            hours.setValue((Integer) m_hours);
            minutes.setValue((Integer) m_mins);
            seconds.setValue((Integer) m_secs);
            label_2.setText(ShutDownDialog.this.AmountOfTime.toString());
            if (ShutDownDialog.this.AmountOfTime< 0)
                {
                timer.stop();
                label_2.setText("stop");
                }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM