2016-03-01 38 views
0

你好吗?我的代码有点问题。当我停止我的定时器程序时,它会说“完成!”因为我将它设置为“完成”,但我想显示剩余时间而不是“完成”一词。例如,如果我的原始时间设置为4000秒,并且我在3000秒停止,则我想在显示当前时间3000时停止它而不是单词“完成”。你们能帮我吗?我真的很感激。我不知道如何在停止时显示计时器

这里是我的代码:

import java.awt.event.*; 
import java.awt.*; 

import javax.swing.*; 
public class CountdownTimer extends JFrame { 

    JLabel promptLabel, timerLabel; 
    int counter; 
    JLabel tf; 
    JButton button; 
    Timer timer; 


    public static void main(String args[]) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new CountdownTimer(); 
      } 

     }); 

    } 


    public CountdownTimer() { 

     JFrame lt = new JFrame(); 
      lt.getContentPane().setBackground(new Color(0, 102, 153)); 
      lt.setBounds(100, 100, 450, 300); 
      lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      lt.getContentPane().setLayout(null); 
      lt.setVisible(true); 

     promptLabel = new JLabel("Time: ", SwingConstants.CENTER); 
     promptLabel.setFont(new Font("Modern No. 20", Font.BOLD, 17)); 
     promptLabel.setBounds(21, 30, 210, 30); 
     lt.add(promptLabel); 

     tf = new JLabel("4000"); 
     tf.setBounds(160, 36, 40, 20); 
     lt.add(tf); 

     button = new JButton("Start"); 
     button.setFont(new Font("Modern No. 20", Font.BOLD, 11)); 
     button.setBounds(299, 37, 89, 23); 
     lt.add(button); 

     JButton btnLogout = new JButton("LOGOUT"); 

     btnLogout.setBackground(new Color(112, 128, 144)); 
     btnLogout.setFont(new Font("Trajan Pro", Font.BOLD, 17)); 
     btnLogout.setBounds(149, 189, 121, 36); 
     btnLogout.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       String sbutt = e.getActionCommand(); 

       if(sbutt.equals("LOGOUT")) { 


        long current = counter; 
        long elapsed = current - counter; 

         timer.stop(); 
         timerLabel.setText("Done!"); 
         Toolkit.getDefaultToolkit().beep(); 
         tf.setText(String.valueOf(elapsed)); 

       } 
      } 
     }); 
     lt.getContentPane().add(btnLogout); 

     timerLabel = new JLabel("Waiting...", SwingConstants.CENTER); 
     timerLabel.setFont(new Font("Modern No. 20", Font.BOLD, 17)); 
     timerLabel.setBounds(119, 97, 176, 62); 
     lt.getContentPane().add(timerLabel); 

     event e = new event(); 
     button.addActionListener(e); 
    } 
    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
    public void run() { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
    public class event implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      int count = (int)(Double.parseDouble(tf.getText())); 
      timerLabel.setText("Time left: " + count); 
      TimeClass tc = new TimeClass(count); 
      timer = new Timer(1000, tc); 
      timer.start(); 
     } 
    } 
    public class TimeClass implements ActionListener { 
     int counter; 
     public TimeClass(int counter) { 
      this.counter= counter; 
    } 
     public void actionPerformed(ActionEvent tc) { 
      counter--; 
      if(counter >= 1) { 
       timerLabel.setText("Time left: " + counter); 
      } 
      else { 
       timer.stop(); 
       timerLabel.setText("Done!"); 
       Toolkit.getDefaultToolkit().beep(); 
      } 
     } 
    } 
} 
+1

变化'timerLabel.setText(“DONE!”);'来'timerLabel.setText(计数器);'? – MadProgrammer

+1

@MadProgrammer:它应该显而易见 - 我不得不怀疑是谁创建了这个代码。 –

回答

1

,但我想显示剩余时间,而不是单词“完成”。

变化timerLabel.setText("Done!");timerLabel.setText(counter);

我应该指出,由于它的本质,Swing Timer不完全准确,您应该关注已经过去的时间量,而不是定时器被调用的频率。当然,这涉及完全归结到你想要如何精确的计数是当然的,但它是值得了解的(恕我直言)

Timer

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public static class TestPane extends JPanel { 

     private JLabel label; 
     private JButton button; 

     private Timer timer; 
     private long startedAt; 

     public static final int RUN_TIME = 5000; 

     public TestPane() { 
      label = new JLabel("..."); 
      button = new JButton("Start"); 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 

      add(label, gbc); 
      add(button, gbc); 

      button.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (timer == null) { 
         button.setText("Stop"); 
         startedAt = System.currentTimeMillis(); 
         timer = new Timer(500, new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent e) { 
           long diff = System.currentTimeMillis() - startedAt; 
           if (diff >= RUN_TIME) { 
            label.setText("Out of time; Used: " + remainingTime() + "s; remaining " + usedTime() + "s"); 
            timer.stop(); 
            button.setText("Start"); 
           } else { 
            label.setText("Used: " + remainingTime() + "s; remaining " + usedTime() + "s"); 
           } 
          } 
         }); 
         label.setText("Used: " + remainingTime() + "s; remaining " + usedTime() + "s"); 
         timer.start(); 
        } else { 
         timer.stop(); 
         long diff = System.currentTimeMillis() - startedAt; 
         label.setText("Used: " + remainingTime() + "s; remaining " + usedTime() + "s"); 
         button.setText("Start"); 
         timer = null; 
        } 
       } 
      }); 
     } 

     public int remainingTime() { 
      long diff = System.currentTimeMillis() - startedAt; 
      return (int) Math.round((RUN_TIME - diff)/1000d); 
     } 

     public int usedTime() { 
      long diff = System.currentTimeMillis() - startedAt; 
      return (int) Math.round(diff/1000d); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(300, 200); 
     } 

    } 

} 
+0

感谢疯狂程序员! =)我真的很感激它! =) –