import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StopWatch extends JFrame //继承父类
{
JPanel mainPane,disPane,ctrPane,lPane; //定义用于界面布局的面板
JButton startbutton,pausebutton,stopbutton,rebutton; //按纽
JLabel[] dislabel;
JLabel[] clabel;
JLabel plabel;
Timer[] t;
boolean flag=true;//
int counts=0;
int size=100;
int MaxSize;
int ms=0,s=0,m=0,h=0;
String input;
boolean ch=false;
public StopWatch()
{
mainPane=new JPanel();
setContentPane(mainPane);
mainPane.setLayout(new BoxLayout(mainPane,BoxLayout.Y_AXIS));
disPane=new JPanel();
mainPane.add(disPane);
disPane.setSize(250,100);
disPane.setLayout(new GridLayout(1,7,2,2));//创建布局管理器,参数分别代表行,列,组件间的水平,垂直间距(在awt包中)
ctrPane=new JPanel();
mainPane.add(ctrPane);
ctrPane.setSize(250,100);
ctrPane.setLayout(new GridLayout(1,4,10,10));
input=JOptionPane.showInputDialog(this,"Please input the number");//弹出对话框要求输入测试次数
if(input==null)
{
System.exit(0);
}
MaxSize=Integer.parseInt(input); //最大测试次数为输入的数字
dislabel=new JLabel[4];
for(int i=0;i<4;i++)
{
dislabel[i]=new JLabel("00");
}
clabel=new JLabel[2];
for(int i=0;i<2;i++)
{
clabel[i]=new JLabel(":");
}
plabel=new JLabel(".");
disPane.add(dislabel[3]);
disPane.add(clabel[1]);
disPane.add(dislabel[2]);
disPane.add(clabel[0]);
disPane.add(dislabel[1]);
disPane.add(plabel);
disPane.add(dislabel[0]);
lPane=new JPanel();
mainPane.add(lPane);
lPane.setLayout(new BoxLayout(lPane,BoxLayout.Y_AXIS));
startbutton=new JButton("Start");
startbutton.setSize(50,30);
startbutton.addActionListener(new Start()); //注册
pausebutton=new JButton("Pause");
pausebutton.setSize(50,30);
pausebutton.addActionListener(new Pause());
stopbutton=new JButton("Stop");
stopbutton.setSize(50,30);
stopbutton.addActionListener(new Stop());
rebutton=new JButton("Reset");
rebutton.setSize(50,30);
rebutton.addActionListener(new Reset());
ctrPane.add(startbutton);
ctrPane.add(pausebutton);
ctrPane.add(stopbutton);
ctrPane.add(rebutton);
t=new Timer[4];
t[0]=new Timer(10,new TimerListener());
t[1]=new Timer(1000,new TimerListener());
t[2]=new Timer(60000,new TimerListener());
t[3]=new Timer(3600000,new TimerListener());
setTitle("StopWatch");
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //框架关闭时结束
setLocation(200,200);
setSize(350,80);
//pack();
validate();
}
class Start implements ActionListener //实现监听器接口
{
public void actionPerformed(ActionEvent e)
{
flag=true;
t[0].start();
ch=true;
if(counts==MaxSize)
{
lPane.removeAll();
setSize(350,80);
size=100;
counts=0;
}
validate();
}
}
class Pause implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
flag=false;
for(int i=0;i<4;i++)
{
t[i].stop();
}
}
}
class Stop implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(ch==true && flag)
{
counts++; //对测试次数进行记数
lp p=new lp();
size=size+35;
setSize(350,size);
p.ldis.setText("No."+counts+" : "+h+":"+m+":"+s+"."+ms);//显示记时结果
lPane.add(p);
validate();
if(counts==MaxSize)
{
dislabel[0].setText("00");
dislabel[1].setText("00");
dislabel[2].setText("00");
dislabel[3].setText("00");
t[0].stop();
t[1].stop();
t[2].stop();
t[3].stop();
counts=MaxSize;
ch=false;
ms=0;s=0;m=0;h=0;
}
}
}
}
class Reset implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
new StopWatch();
}
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ms++;
dislabel[0].setText(""+ms);
if(ms==99)
{
t[1].start();
s++;
dislabel[1].setText(""+s);
ms=0;
if(s==59)
{
t[2].start();
m++;
dislabel[2].setText(""+m);
s=0;
if(m==59)
{
t[3].start();
h++;
dislabel[4].setText(""+h);
m=0;
}
}
}
}
}
public static void main(String[] args)
{
new StopWatch();
}
class lp extends JPanel
{
JTextField ldis;
lp()
{
ldis=new JTextField();
ldis.setEditable(false);
this.setSize(250,20);
this.add(ldis);
}
}
}

tianpan000
- 粉丝: 12
最新资源
- 地铁车站大跨度深基坑支护技术的研究与应用.doc
- 通信设备工程验收规范.doc
- 内审检查表(QR-8.2-05).docx
- 海岛度假村岩土工程勘察.doc
- 造价员入门--桩与地基基础工程.ppt
- 基于JSP在线音乐网的方案设计书与实现大学本科方案设计书.doc
- 5G时代背景下教师网络研修的创新探究.docx
- 数据库系统概论期末试题及复习资料重点知识.doc
- 第4章ANSYS边坡工程应用实例分析.doc
- OAuth2学习DotNetOpenAuth部分源码研究分析.doc
- 《建筑工程计价》模拟试卷答案-杭州.doc
- 如何使用PHOTOSHOP处理图片让宝贝图片更清晰.doc
- 某广场钢筋工技术交底.doc
- 电子工程自动化控制智能技术研究.docx
- VB超市管理系统毕业设计论文.doc
- 5幕墙技术保证措施.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论1