...
JTextField txtTime = new JTextField();
...
 Date date = new Date();
 DateFormat df = DateFormat.getDateTimeInstance();
txtTime.setText=(df.format(date));怎么才能让时间动起来 最好用runnable接口实现  求大虾帮帮忙  我只有10分了

解决方案 »

  1.   

    import java.awt.*;public class MyTimer extends Frame {
      TextField tf = new TextField();
      
      public launch() {
        add(tf);
        pack();
        setVisible(true);
        new Thread(new MyThread()).start();
      }  public static void main(String[] args) {
        new MyTimer().launch();
      }
      
      class MyThread implements Runnable {
        public void run () {
          while(true) {
            Date date = new Date();
            DateFormat df = DateFormat.getDateTimeInstance();
            txtTime.setText=(df.format(date));
            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {}
          }
        }
      }}直接写的,没有经过测试,有问题找我.**** http://www.bjsxt.com ****
    **** 北京尚学堂科技java培训 , 为大家服务 , 顺便做点广告 , 请见谅! ****
      

  2.   

    Timer不错,但最好将其设置为守护线程,防止关闭时无法退出
      

  3.   

    import javax.swing.*;
    import java.util.concurrent.*;
    import java.util.*;public class  ScheduledExecutorTest{
        public static void main(String[] args){
    JFrame frame=new JFrame();
    JTextField text=new JTextField();
    frame.add(text);
    ScheduledExecutorService service=Executors.newScheduledThreadPool(1);
    service.scheduleWithFixedDelay(new 
            DateRun(text),1,1,TimeUnit.SECONDS);
            frame.pack();
    frame.setVisible(true);

        }


    }class DateRun implements Runnable{
         private JTextField text;
            public DateRun(final JTextField atext){
            text=atext;
         }

        public void run(){
           text.setText(new Date().toString());
        }
    }