importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.text.SimpleDateFormat;importjava.util.Date;importjavax.swing.Timer;public...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Timer;
public class timer implements ActionListener{
Button button1;
Button button2;
Timer secondTime,secondTime2;
private Frame frame;
Label label1=new Label();
Panel p;
Panel q;
int n=10;
public timer()
{
this.p=new Panel();
p.add(label1);
frame=new Frame("秒表");/*创建窗体*/
this.button1=new Button("点击");
button1.addActionListener(this);
frame.add(button1,BorderLayout.SOUTH);
p.setVisible(true);
frame.add(p,BorderLayout.CENTER);
this.button2=new Button("暂停");
button2.addActionListener(this);
frame.add(button2,BorderLayout.SOUTH);
q.setVisible(true);
frame.add(q,BorderLayout.CENTER);
secondTime2 = new Timer(10000,this);/*将定时器设定为每秒执行一次actionPerformed的方法*/
secondTime = new Timer(10000,this);/*将定时器设定为每秒执行一次actionPerformed的方法*/
frame.setBounds(400,400,400,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)/*触发开始测试菜单项时发生的事件*/
{
secondTime.start();/*定时器启动*/
secondTime2.start();/*定时器启动*//*定时器启动*/}
else if(e.getSource()==button2)
{
secondTime.stop();
secondTime2.stop();
}
else if(e.getSource()==secondTime)
{
System.exit(0);
}
else if(e.getSource()==secondTime2)
{
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
String str = dateFormat.format( now );
label1.setText(str);
}
}
public static void main(String[] args){
timer a=new timer();
}}
展开