我的DigitalClock无法正常工作

时间:2013-08-18 04:27:08

标签: java clock

我做了一个数字时钟。但它不能正常工作。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class DigitalClock extends JFrame implements ActionListener {
  JLabel  l1 = new JLabel();
  Timer  t;
  public  DigitalClock() {
     super("Digital Clock");
     l1.setFont( new Font("Verdana",Font.BOLD,11) );
     l1.setHorizontalAlignment( JLabel.RIGHT);
     l1.setVerticalAlignment( JLabel.BOTTOM);
     t = new Timer(1000,this);
     getContentPane().add(l1);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     setSize(110,100);
     setVisible(true);
     // call actionPerformed to get Time at the startup
     actionPerformed(null);
  }
  public void actionPerformed(ActionEvent evt)  {
      l1.setText( new Date().toString().substring(11,19));
  }
  public static void main(String args[]) {
     new DigitalClock();
  }
} // end of class

我做了一个数字时钟。但它不能正常工作。
帮助我,我找不到问题..
请帮忙.. output is constant time

2 个答案:

答案 0 :(得分:3)

创建Time obj后,只需启动计时器 t.start();

答案 1 :(得分:2)

  输出时间

恒定不动。但我想继续时钟

你启动了计时器吗?

如果您启动计时器,则您甚至不需要:

// actionPerformed(null);