当前位置: 代码迷 >> Java相关 >> 新人求助,帮忙解决下我的时钟程序!!
  详细解决方案

新人求助,帮忙解决下我的时钟程序!!

热度:89   发布时间:2011-12-08 19:52:40.0
新人求助,帮忙解决下我的时钟程序!!
自己编的时钟程序,为什么不能用按钮停止时间呢?代码如下
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class DDate extends Applet implements ActionListener,Runnable{
    Button b1=new Button("显示时间");
    Button b2=new Button("停止时间");
    Label l=new Label("显示当前时间");
    Thread t;
    boolean flag=true;
    public void init(){
        setLayout(new BorderLayout());
        add(b1,BorderLayout.NORTH);
        add(b2,BorderLayout.SOUTH);
        add(l,BorderLayout.CENTER);
        b1.addActionListener(this);
        b2.addActionListener(this);
        
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("显示时间")) {   
            while(flag=true){
                Date now=new Date();
                l.setText("现在是北京时间:"+now.getHours()+"时"+now.getMinutes()+"分"+now.getSeconds()+"秒");
                try{Thread.sleep(1000);}catch(InterruptedException f){}
            }
    }
        if (e.getActionCommand().equals("停止时间")) {   
            t.stop();
        }
}
    public void start(){        
        t=new Thread(this);
        t.start();
    }
    public void run(){
        while(true){
        Date now=new Date();
        System.out.println("现在是北京时间:"+now.getHours()+"时"+now.getMinutes()+"分"+now.getSeconds()+"秒");
        try{Thread.sleep(1000);}catch(InterruptedException e){}
        }
    }
}
搜索更多相关的解决方案: class  public  import  

----------------解决方案--------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class DDate extends Applet implements ActionListener,Runnable{
     Button b1=new Button("显示时间");
     Button b2=new Button("停止时间");
     Label l=new Label("显示当前时间");
     Thread t;
     boolean flag=true;
     public void init(){
         setLayout(new BorderLayout());
         add(b1,BorderLayout.NORTH);
         add(b2,BorderLayout.SOUTH);
         add(l,BorderLayout.CENTER);
         b1.addActionListener(this);
         b2.addActionListener(this);
         
     }
     public void actionPerformed(ActionEvent e) {
         if (e.getActionCommand().equals("显示时间")) {   
           flag=true;
           start();
     }
         if (e.getActionCommand().equals("停止时间")) {   
            flag=false;
         }
         
}
     public void start()
     {        
        
            t=new Thread(this);
            
         
         t.start();
         
     }
     public void run(){
         while(t!=null&&flag)
         {
         
           Date now=new Date();
           l.setText("现在是北京时间:"+now.getHours()+"时"+now.getMinutes()+"分"+now.getSeconds()+"秒");

           try{Thread.sleep(1000);}catch(InterruptedException e){}
         }
     }
}
----------------解决方案--------------------------------------------------------
我太新手了。。。。。还看不懂
----------------解决方案--------------------------------------------------------
  相关解决方案