| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 477 人关注过本帖
标题:新人求助,帮忙解决下我的时钟程序!!
只看楼主 加入收藏
guillerns20
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-8
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
新人求助,帮忙解决下我的时钟程序!!
自己编的时钟程序,为什么不能用按钮停止时间呢?代码如下
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 
2011-12-08 19:52
linMop
Rank: 1
等 级:新手上路
帖 子:6
专家分:7
注 册:2011-12-9
收藏
得分:5 
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){}
         }
     }
 }
2011-12-10 22:11
lvmp520
Rank: 2
等 级:论坛游民
威 望:1
帖 子:18
专家分:24
注 册:2011-11-12
收藏
得分:5 
我太新手了。。。。。还看不懂
2011-12-11 09:15
快速回复:新人求助,帮忙解决下我的时钟程序!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011503 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved