java计时器没有递增

时间:2013-10-25 21:41:10

标签: java swing timer

我尝试在Java中创建一个显示在JFrame窗口中的计时器,但我没有运气!

计时器始终保持为0.如果有人能看到我出错的地方,我将不胜感激:(

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Globals 
{
    public static int seconds = 0;
}

class main
{
    public static void main(String Args[])
    {

        JFrame testing = new JFrame();
        testing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testing.setSize(1000,1000);
        testing.setVisible(true);

        Countdown timer = new Countdown();
        Timer countdown = new Timer(5000, timer);
        countdown.start();

        JLabel firstline = new JLabel();
        firstline.setText("Time is:" + Globals.seconds);
        testing.add(firstline);

    }
}

class Countdown implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
    Globals.seconds++;

    }
}

2 个答案:

答案 0 :(得分:2)

您可以在此处设置JLabel的文字:

JLabel firstline = new JLabel();
firstline.setText("Time is:" + Globals.seconds);

但是一旦确定了它,你永远不会改变它。

所有计时器都会改变变量的状态。它对GUI没有任何作用,例如告诉JLabel使用新值。请理解,JLabel不会神奇地跟踪用于创建其文本字符串的任何变量。它只知道它已被设置为不可变的字符串,就是这样。

你的Timer应该调用JLabel上的setText(...)来更新它的文本,或者更好的是,使用对持有JLabel的类的引用,并调用该类的公共方法来改变JLabel的状态。

例如:

import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

public class TimerTest {
   private static final int TIMER_DELAY = 20;

   private static void createAndShowGui() {
      MyGui myGui = new MyGui();
      TimerListener timerListener = new TimerListener(myGui, 2 * 60 * 1000);
      new Timer(TIMER_DELAY, timerListener).start();
      JFrame frame = new JFrame("TimerTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(myGui.getMainComponent());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyGui {
   private static final int PREF_W = 200;
   private static final int PREF_H = 80;
   private JPanel mainPanel = new JPanel() {
      public Dimension getPreferredSize() {
         return MyGui.this.getPreferredSize();
      };
   };
   private JLabel statusLabel = new JLabel();

   public MyGui() {
      mainPanel.add(statusLabel);
   }

   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   public void setStatus(String text) {
      statusLabel.setText(text);
   }
}

class TimerListener implements ActionListener {
   private static final String FORMAT = "Count Down: %02d:%02d:%03d";
   private static final int MS_PER_SEC = 1000;
   private static final int SEC_PER_MIN = 60;
   private MyGui myGui;
   private long countDownTime;
   private long startTime;

   public TimerListener(MyGui myGui, long countDownTime) {
      this.myGui = myGui;
      this.countDownTime = countDownTime;
      startTime = System.currentTimeMillis();
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      long currentTime = System.currentTimeMillis();
      long deltaTime = countDownTime - (currentTime - startTime);
      if (deltaTime < 0) {
         ((Timer) evt.getSource()).stop();
         deltaTime = 0;
      }
      int minutes = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN));
      deltaTime = deltaTime % (MS_PER_SEC * SEC_PER_MIN);
      int seconds = (int) (deltaTime / MS_PER_SEC);
      deltaTime = deltaTime % MS_PER_SEC;
      int msecs = (int) deltaTime;
      String text = String.format(FORMAT, minutes, seconds, msecs);
      myGui.setStatus(text);
   }
}

答案 1 :(得分:2)

计时器启动,等待5秒钟。

在这5秒钟内,它运行以下代码:

JLabel firstline = new JLabel();
firstline.setText("Time is:" + Globals.seconds);
testing.add(firstline);

然后它会增加你的价值。

由于上面的代码不在循环或任何东西中,它永远不会再运行它。

也许尝试将JLabel更新放在计时器本身中:

class Countdown implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        Globals.seconds++;
        main.firstline.setText("Time is:" + Globals.seconds);
    }
}

显然你需要在这里访问JLabel,我会留下你的意思。