好像没有地方,重画窗口,每次label变了要强制调用repaint(),否则label不会变。你每次改变窗口大小时,label就会变的!

解决方案 »

  1.   

    每一次
    L.setText("XXXXXXXX");

    L.repaint();
      

  2.   

    ////////////////////////////////////////做了如下修改
    void btn_Clock_actionPerformed(ActionEvent e) {
            ClockThread nowClock=new ClockThread();
            nowClock.setLabel(jLabel1);
            nowClock.start();
    /*        jLabel1.setText(nowClock.repaint());
            Date now=new Date();
            String s;
            s=now.getHours()+":"+now.getMinutes() +":"+now.getSeconds();
            jLabel1.setText(s);*/
        }
    }class ClockThread extends Thread {
        private Thread clockTh=null;
        JLabel L;
        public  void  setLabel(JLabel L)
        {
          this.L = L;
        }
        /*
        public void start(){        if(clockTh==null){
                //clockTh=new Thread(this,"ClockThread");
                clockTh.start();
            }    }
        */
        public void run(){
            //Thread myThread=Thread.currentThread();
            //start();
            //System.out.println("currentThread = "+myThread);
            System.out.println("this = "+this);
            //System.out.println("clockTh = "+clockTh);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            L.setText("Zhangping");
        /*        try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
                L.setText("23:59:59");*/        //while(clockTh==myThread){
              while  (true)
              {
                L.setText(repaint());
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
              }
               // //i//f(clockTh.getThreadGroup()!=myThread.getThreadGroup())
                  //  System.out.println("Flase");
                //e//lse
                   // System.out.println("True");
                //}
            }
        public String repaint(){
            String s;
            Date now=new Date();
            s=now.getHours() +":"+now.getMinutes()+":"+now.getSeconds();
            return s;
        }
    }
      

  3.   

    也许不太规范,但你所要的功能是实现了:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.*;
    import javax.swing.*;
    import java.lang.Thread;
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class Clock {    ClockThread nowClock=new ClockThread();    private static JPanel jPanel1 = new JPanel();
        private JLabel jLabel1 = new JLabel();
        private JButton btn_Start = new JButton();
        private JButton btn_Clean = new JButton();
        private JButton btn_Clock = new JButton();
        private BorderLayout borderLayout1 = new BorderLayout();
        private TitledBorder titledBorder1;    public Clock() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception {
            titledBorder1 = new TitledBorder("");
            jLabel1.setBackground(Color.white);
            jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
            jLabel1.setForeground(Color.red);
            jLabel1.setBorder(titledBorder1);
            jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
            jLabel1.setText("jLabel1");
            btn_Start.setBackground(Color.orange);
            btn_Start.setFont(new java.awt.Font("Dialog", 0, 14));
            btn_Start.setForeground(Color.red);
            btn_Start.setBorder(titledBorder1);
            btn_Start.setToolTipText("");
            btn_Start.setMnemonic('S');
            btn_Start.setText("开始(S)");
            btn_Start.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn_Start_actionPerformed(e);
                }
            });
            btn_Clean.setBackground(Color.orange);
            btn_Clean.setFont(new java.awt.Font("Dialog", 0, 14));
            btn_Clean.setForeground(Color.red);
            btn_Clean.setBorder(titledBorder1);
            btn_Clean.setMnemonic('C');
            btn_Clean.setText(" 清零(C)");
            btn_Clean.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn_Clean_actionPerformed(e);
                }
            });
            btn_Clock.setBackground(Color.orange);
            btn_Clock.setFont(new java.awt.Font("Dialog", 0, 14));
            btn_Clock.setForeground(Color.magenta);
            btn_Clock.setBorder(titledBorder1);
            btn_Clock.setMnemonic('O');
            btn_Clock.setText(" 时钟(O)");
            btn_Clock.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn_Clock_actionPerformed(e);
                }
            });
            jPanel1.setLayout(borderLayout1);
            borderLayout1.setHgap(2);
            borderLayout1.setVgap(2);
            jPanel1.setEnabled(true);
            jPanel1.add(jLabel1, BorderLayout.NORTH);
            jPanel1.add(btn_Start, BorderLayout.WEST);
            jPanel1.add(btn_Clean, BorderLayout.CENTER);
            jPanel1.add(btn_Clock, BorderLayout.EAST);//        ClockThread nowClock=new ClockThread();
        }
    /*    public void start(){
            Thread clockThread;
            if(clockThread==null){
                clockThread=new java.lang.Thread(this,"Clock");
                clockThread.start();
            }
        }*/
        public static void main(String args[]){
            JFrame frame=new JFrame("nihao");
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
            new Clock();
            frame.getContentPane().add(jPanel1,BorderLayout.CENTER );
            frame.pack();
            frame.setVisible(true);
        }    void btn_Start_actionPerformed(ActionEvent e) {
            jLabel1.setText("nihao");
            try{
            nowClock.suspend();
            }
            catch(Exception e1) {
              e1.printStackTrace();
            }
        }    void btn_Clean_actionPerformed(ActionEvent e) {
            jLabel1.setText("00:00:00");
            try{
            nowClock.suspend();
            }
            catch(Exception e1) {
              e1.printStackTrace();
            }
        }void btn_Clock_actionPerformed(ActionEvent e) {
    //        ClockThread nowClock=new ClockThread();
            nowClock.setLabel(jLabel1);
            if(nowClock.isAlive()) nowClock.resume();
            else  nowClock.start();
    /*        jLabel1.setText(nowClock.repaint());
            Date now=new Date();
            String s;
            s=now.getHours()+":"+now.getMinutes() +":"+now.getSeconds();
            jLabel1.setText(s);*/
        }
    }class ClockThread extends Thread {
        private Thread clockTh=null;
        JLabel L;
        public  void  setLabel(JLabel L)
        {
          this.L = L;
        }
        /*
        public void start(){        if(clockTh==null){
                //clockTh=new Thread(this,"ClockThread");
                clockTh.start();
            }    }
        */
        public void run(){
            //Thread myThread=Thread.currentThread();
            //start();
            //System.out.println("currentThread = "+myThread);
            //System.out.println("this = "+this);
            //System.out.println("clockTh = "+clockTh);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            L.setText("Zhangping");
        /*        try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
                L.setText("23:59:59");*/        //while(clockTh==myThread){
              while  (true)
              {
                L.setText(repaint());
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
              }
               // //i//f(clockTh.getThreadGroup()!=myThread.getThreadGroup())
                  //  System.out.println("Flase");
                //e//lse
                   // System.out.println("True");
                //}
            }
        public String repaint(){
            String s;
            Date now=new Date();
            s=now.getHours() +":"+now.getMinutes()+":"+now.getSeconds();
            return s;
        }
    }