小弟刚学JAVA
想做一个数字的时钟.怎么样才能时取得的时间是在一秒一秒的走动的呢??

解决方案 »

  1.   

    jdk目录/demo/applet/clock是一个图形时钟,你把它改成数字的就可以了
      

  2.   

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowEvent;
    import java.util.Date;
    import java.text.SimpleDateFormat;public class ClockFrame extends JFrame implements Runnable,WindowListener
    {
    private Thread timer=null;
    private JLabel jLabel=null;
    private SimpleDateFormat sdf=null;
    private boolean go; public ClockFrame()
    {
    super("时钟测试"); sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date now=new Date(); jLabel=new JLabel(sdf.format(now));
    getContentPane().add(jLabel,BorderLayout.CENTER);
    addWindowListener(this);
    setSize(100,50);
    setLocation(300,200); go=true;
    timer=new Thread(this);
    timer.start();
    } public void run()
    {
    while(go)
    {
    try
    {
    timer.sleep(1000);
    }catch(InterruptedException e){
    e.printStackTrace();
    }finally
    {
    jLabel.setText(sdf.format(new Date()));
    }
    }
    }
    public void windowActivated(WindowEvent e){}
    public void windowClosed(WindowEvent e){}
    public void windowClosing(WindowEvent e){
    go=false;
    setVisible(false);
    System.exit(0);
    }
    public void windowDeactivated(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowOpened(WindowEvent e){} public static void main(String args[])
    {
    new ClockFrame().setVisible(true);
    }
    };