package pro;import javax.swing.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
public class Clock  extends JFrame {
    
    public Clock(){
     this.setTitle("测试时间");
     this.setResizable(false);
     this.setSize(500,500);
     this.setLocationRelativeTo(null);
        try {
            jbInit();
            this.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        Clock clock = new Clock();
    }    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        jLabel1.setFont(new java.awt.Font("幼圆", Font.BOLD, 50));
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(new Rectangle(19, 94, 521, 86));
        thread ks = new thread(this);
        ks.start();
        ks.Run();
        this.getContentPane().add(jLabel1);    }    JLabel jLabel1 = new JLabel();}class thread extends Thread{
            Clock c = null;
            public thread(Clock c){
                this.c = c;
            }            public void Run(){
                Date da = new Date();
                this.c.jLabel1.setText(da.toLocaleString());
                //System.out.println(da.toLocaleString());
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException ex) {
                }
               Run();
            }
        }

解决方案 »

  1.   

    已经解决。。 public void run(){ 
                    Date da = new Date(); 
                    this.c.jLabel1.setText(da.toLocaleString()); 
                    //System.out.println(da.toLocaleString()); 
                    try { 
                        Thread.currentThread().sleep(1000); 
                        Thread.currentThread().run();
                    } catch (InterruptedException ex) { 
                    } 
                    
      

  2.   

    建议不要在事件分配线程中做很多事情,需要的话另起线程。如果另起的线程需要更新界面,建议使用EventQueue的invokeLater等方法