您在此处设置 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);
}
}