| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 477 人关注过本帖
标题:新人求助,帮忙解决下我的时钟程序!!
取消只看楼主 加入收藏
guillerns20
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-8
结帖率:0
收藏
已结贴  问题点数:10 回复次数: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 
2011-12-08 19:52
快速回复:新人求助,帮忙解决下我的时钟程序!!
数据加载中...
 
   



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

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