如何实现一个秒表的功能,在一个文本框中显示即时时间,象秒表那样,时间是时刻变化的,请给出源代码

解决方案 »

  1.   

    提示你用Calendar.getInstance().getTime()返回当前时间。如果你用JPanel的话,记得在paintComponent()中刷新版面。
      

  2.   

    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;public class ShowTime extends JFrame{
    private JTextField time;
    public ShowTime(){
    time = new JTextField(20);
    this.add(time);
    this.setSize(200,150);
    this.setLocation(100,100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void refreshTime(){
    Date date = new Date();
    time.setText(date.toString());
    }

    public static void main(String args[]){
    final ShowTime showTime = new ShowTime();
    showTime.setVisible(true);

    Timer t = new Timer(1000,new ActionListener(){
    public void actionPerformed(ActionEvent e){
    showTime.refreshTime();
    }
    });

    t.start();
    }
    }