我不太了解您为什么使用随机数的问题,但以下是一些观察结果:
我想每秒钟更新一次带有倒计时的JLabel。
然后,您需要将计时器设置为每秒触发一次。因此,计时器的参数是1000,而不是一些随机数。
另外,在您的actionPerformed()方法中,您将在首次触发计时器时停止计时器。如果您要进行某种倒计时,则只有在时间到0时才停止计时器。
这是一个使用计时器的简单示例。它只是每秒更新一次时间:
import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.Timer;public class TimerTime extends JPanel implements ActionListener{ private JLabel timeLabel; public TimerTime() { timeLabel = new JLabel( new Date().toString() ); add( timeLabel ); Timer timer = new Timer(1000, this); timer.setInitialDelay(1); timer.start(); } @Override public void actionPerformed(ActionEvent e) { //System.out.println(e.getSource()); timeLabel.setText( new Date().toString() ); } private static void createAndShowUI() { Jframe frame = new Jframe("TimerTime"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.add( new TimerTime() ); frame.setLocationByPlatform( true ); frame.pack(); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); }}如果您需要更多帮助,请使用适当的SSCCE演示问题来更新您的问题。所有问题均应具有适当的SSCCE,而不仅仅是几行随机的代码,以便我们可以理解代码的上下文。



