public class Test extends Frame{
private boolean bStop1;
private boolean bStop2;private Button btn;
private Label label1;
private Label label2;class Refresh implements Runnable{
Label label;
public Refresh(Label l){
label = l;
}
public void run(){
while (!bStop1){
try{
  Thread.sleep(1000);
}
catch (InterruptedException ie){
  break;
}
EventQueue.invokeLater(new Runnable(){
public void run(){
String num = label.getText();
try{
  num = new String(Integer.parseInt(num)+1);
}
catch (NumberFormatException nfe){
}
label.setText(num);
});
}
}public Test(){
bStop1 = true;
bStop2 = true;
btn = new Button("Click me");
label1 = new Label("1");
label2 = new Label("2");btn.addActionListener(new ActionListener(){
pubilc void actionPerformed(ActionEvent ae){
if (bStop1){
bStop1 = false;
Thread t = new Thread(new Refresh(label1);
t.start();}
else if (bStop2){
bStop2 = false;
Thread t = new Thread(new Refresh(label2);
t.start();
}
else{
//所有线程已经在运行
bStop1=true;
bStop2=true;
}
});add(label1);
add(label2);
add(btn);addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Test.this.dispose();
System.exit(1);
}
});}

解决方案 »

  1.   

    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;public class Clock {
        private static JPanel jPanel1 = new JPanel();
        private JLabel jLabel1 = new JLabel();
        private JButton btn_Clock1 = new JButton();
        private JButton btn_Clock2 = new JButton();
        private TitledBorder titledBorder1;
        GridLayout gridLayout1 = new GridLayout();
        JLabel jLabel2 = new JLabel();
        TitledBorder titledBorder2;
        ClockThread nowClock1 = new ClockThread();
        ClockThread nowClock2 = new ClockThread();
        public Clock() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception {
            titledBorder1 = new TitledBorder("");
            titledBorder2 = 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("00:00:00");
            btn_Clock1.setBackground(Color.orange);
            btn_Clock1.setFont(new java.awt.Font("Dialog", 0, 14));
            btn_Clock1.setForeground(Color.red);
            btn_Clock1.setBorder(titledBorder1);
            btn_Clock1.setMnemonic('C');
            btn_Clock1.setText("时钟(一)");
            btn_Clock1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn_Clock1_actionPerformed(e);
                }
            });
            btn_Clock2.setBackground(Color.orange);
            btn_Clock2.setFont(new java.awt.Font("Dialog", 0, 14));
            btn_Clock2.setForeground(Color.red);
            btn_Clock2.setBorder(titledBorder1);
            btn_Clock2.setMnemonic('O');
            btn_Clock2.setText(" 时钟(二)");
            btn_Clock2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn_Clock2_actionPerformed(e);
                }
            });
            jPanel1.setLayout(gridLayout1);
            jPanel1.setEnabled(true);
            gridLayout1.setRows(2);
            gridLayout1.setColumns(2);
            jLabel2.setBackground(Color.white);
            jLabel2.setForeground(Color.red);
            jLabel2.setBorder(titledBorder2);
            jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
            jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
            jLabel2.setText("00:00:00");
            jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
            jPanel1.add(jLabel1, null);
            jPanel1.add(jLabel2, null);
            jPanel1.add(btn_Clock1, null);
            jPanel1.add(btn_Clock2, null);
            nowClock1.setLabel(jLabel1);
            nowClock2.setLabel(jLabel2);
        }
        public static void main(String args[]){
            JFrame frame=new JFrame("Clock");
            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_Clock1_actionPerformed(ActionEvent e) {
            jLabel2.setText("00:00:00");
            try{
            if(nowClock1.isAlive()) nowClock1.resume();
            else nowClock1.start();
            if(nowClock2.isAlive()) nowClock2.suspend();
            }
            catch(Exception e1) {
              e1.printStackTrace();
            }
        }void btn_Clock2_actionPerformed(ActionEvent e) {
            jLabel1.setText("00:00:00");
            try{
            if(nowClock2.isAlive()) nowClock2.resume();
            else nowClock2.start();
            if(nowClock1.isAlive()) nowClock1.suspend();
            }
            catch(Exception e1) {
              e1.printStackTrace();
            }
        }
    }class ClockThread extends Thread {
        private Thread clockTh = null;
          JLabel L;
        public  void  setLabel(JLabel L)
        {
          this.L = L;
        }
        public void run(){
              while  (true)
              {
                L.setText(repaint());
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
              }
            }
        public String repaint(){
            String s;
            Date now = new Date();
            s=now.getHours() +":"+now.getMinutes()+":"+now.getSeconds();
            return s;
        }
    }
    程序中有些不规范的地方,主要是用了suspend()和resume()方法。今天没时间了,不改了。你可以改用wait()和notify()。