求一完整的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("窗口");
}
}