怎样在JTextField中显示当前时间

解决方案 »

  1.   

    1、写内容 setText
    2、当前时间 
       SimpleDateFormat + Calendar类 = 你所需要格式的当前时间。   
      

  2.   

    我给你说一个经常用的方法。来显示时间的。
    public class Time implements Runnable{
    private JLabel label;
    public Time(JLabel label){
    this.label=label;
    }
    public void run(){
    while(true){
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
    String time=dateFormat.format(new Date());
    label.setText("当前时间:"+time);
    label.setFont(new Font("微软雅黑",Font.BOLD,16));
    try{
    Thread.sleep(1000);
    }catch(Exception e){
    label.setText("系统时间暂时无法显示");
    }
    }
    }
    }
    我法的Demo里边是用JLabel来显示的。LZ可以拿去测试下,这只是一个单纯动态显示当前时间的类
      

  3.   

    final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final String currentTime = formatter.format(new Date());
    final JLabel label = new JLabel(currentTime);
    javax.swing.Timer timer = new javax.swing.Timer(1000,new ActionListener(){
    @Override void actionPerformed(e:ActionEvent)
    {
        label.setText(formatter.format(new Date()));
    }
        });
    timer.start()
      

  4.   


    mport java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    public class Swing_test extends JFrame{
        public Swing_test(){
            this.setSize(300,150);
            this.setLocation(300,200);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            final JTextField jt=new JTextField();
            jt.setSize(150, 20);
            Timer time=new Timer(1000,new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
    jt.setText(DateToString(new Date()));
    }
            });
            time.start();
            this.add(jt);
            this.setVisible(true);
        }
        
    public static String DateToString(Date date) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateStr = format.format(date);
    return dateStr;
    }
        public static void main(String[] ar){
            new Swing_test();
        }
    }