Java计时器(秒表)功能:能实现计时,暂停,清零,记录功能。如下图:程序运行状态:源代码:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTest{publicstaticvoidmain(String[]args){newwindow(计时器);}}classwindowextendsJFrame{intON=0,i=0,j=0,k=0,count=0,num=1,R=0;JButtonbutton1,button2,button3,button4,button5;JTextFieldfile1,file2,file3;JTextAreafile;FlowLayoutflow;Stringa,b,c;window(Stringname){file1=newJTextField(2);file2=newJTextField(2);file3=newJTextField(2);file1.setEditable(false);file2.setEditable(false);file3.setEditable(false);file=newJTextArea(10,8);file.setEditable(false);button1=newJButton(开始);button2=newJButton(暂停);button3=newJButton(清零);button4=newJButton(记录);button5=newJButton(清空记录);flow=newFlowLayout();flow.setAlignment(FlowLayout.LEFT);flow.setHgap(20);flow.setVgap(10);setTitle(name);setSize(210,400);setLayout(flow);add(file1);add(newJLabel(:));add(file2);add(newJLabel(:));add(file3);add(button1);add(button2);add(button3);add(button4);add(button5);add(file);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);file1.setText(0);file2.setText(0);file3.setText(0);validate();button1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){ON=1;//开始,暂停控制开关}});//开始按钮button2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){ON=0;}});//暂停按钮button3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){R=1;//清零控制开关}});//清零按钮button4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){count=1;//记录控制开关a=String.valueOf(i);b=String.valueOf(j);c=String.valueOf(k);}});//记录按钮button5.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){file.setText();num=1;}});//清除记录按钮while(true){validate();if(ON==1)//开始或暂停判断{file1.setText(String.valueOf(i));file2.setText(String.valueOf(j));file3.setText(String.valueOf(k));if(k==99){k=-1;j++;}if(j==60){k=0;j=0;i++;}if(i==24){i=0;j=0;k=0;}try{Thread.sleep(10);}catch(Exceptione){}k++;}if(count==1)//记录判断{file.append(String.valueOf(num));file.append(.);file.append(a);file.append(:);file.append(b);file.append(:);file.append(c);file.append(\n);num++;count=0;}if(R==1)//清零判断{i=j=k=0;file1.setText(String.valueOf(i));file2.setText(String.valueOf(j));file3.setText(String.valueOf(k));R=0;}}}}