| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 601 人关注过本帖
标题:求一完整的Java框架中添加计时器的程序
只看楼主 加入收藏
付冠英
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-12-2
结帖率:50%
收藏
 问题点数:0 回复次数:0 
求一完整的Java框架中添加计时器的程序
不懂计时器是如何添加的,编一程序总是出错不懂 求一完整代码
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
class  WindowButton extends Frame implements ActionListener
{
    int number;
    Calendar cal=new Calendar.getInstance();
    Timer time=new Timer();
    Calendar calendar=Calendar.getInstance();
    Label 提示条;
    TextField 输入框,输出框1,输出框2;//输出框;
    Button buttonGetNumber,buttonEnter1,button1,button2;//buttonEnter2;
    WindowButton(String s)
    {  super(s);
        //time=new Timer(1000,this);//WindowButton对象做计时器的监视对象
       setLayout(new FlowLayout());
       buttonGetNumber=new Button("得到一个随机数");
       add(buttonGetNumber);
       提示条=new Label("输入你的猜想",Label.CENTER);
       提示条.setBackground(Color.cyan);
       输入框=new TextField("0",10);
       //输出框=new TextField(10);
       输出框1=new TextField();
       add(提示条);
       add(输入框);
       buttonEnter1=new Button("确定");
       add(buttonEnter1);      
       button1=new Button("时间");
       add(button1);
       add(输出框1);
       button1=new Button("评价");
       add(button2);
       add(输出框2);
       java.util.Date date=new java.util.Date();
           String str=date.toString().substring(11,19);
           输出框1.setText(str);
     /*  button1.addActionListener(this);
       button2.addActionListener(this);
       add(输出框);
       buttonEnter2=new Button("答案");
       add(buttonEnter2);
       buttonEnter2.addActionListener(this);*/
      
       buttonEnter1.addActionListener(this);
       buttonGetNumber.addActionListener(this);
       setBounds(100,100,150,150);
       setVisible(true);
       validate();
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==buttonGetNumber)
        {  number=(int)(Math.random()*100)+1;
           提示条.setText("输入你的猜测:");
           time.start();
        }
        else if(e.getSource()==buttonEnter1)
        {  int guess=0;
           try
           {
              guess=Integer.parseInt(输入框.getText());
              if(guess==number)
               { 提示条.setText("猜对了!");
                 time.stop();
               }
               else if(guess>number)
               {  提示条.setText("猜大了!");
                  输入框.setText(null);
               }
               else if(guess<number)
               {  提示条.setText("猜小了!");
                  输入框.setText(null);
               }
           }
           catch (NumberFormatException event)
           { 提示条.setText("请输入数字字符");
           }
        }
        /*else if(e.getSource()==buttonEnter2)
            {  int 输出=0;
               输出=number;
               输出框.setText("该随机数为:"+输出);
            }*/
    }
    public void start()
    {   long t1=cal.getTimeInMillis();        
        time.schedule(new TimerTask()
              {  public void run()
                 {  long t2=cal.getTimeInMillis();
                    long m=t2-t1;
                    System.out.println(m+"s");
                  }
               },1000);
    }
}
public class Example7_61
{
    public static void main(String[] args)
    {  WindowButton win=new WindowButton("窗口");
    }
}
搜索更多相关主题的帖子: import number calendar 计时器 
2012-05-25 21:16
快速回复:求一完整的Java框架中添加计时器的程序
数据加载中...
 
   



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

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