0

我尝试在 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++;

    }
}
4

2 回答 2

2

您在此处设置 JLabel 的文本:

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

但是一旦设置好了,你就永远不会改变它。

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

您的 Timer 应该调用setText(...)JLabel 来更新其文本,或者更好的是,使用对持有 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);
   }
}
于 2013-10-25T21:42:53.120 回答
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这里,我将把它留给你。

于 2013-10-25T21:44:25.790 回答