秒表java程序代码 java秒表程序设计

Java做的秒表: 代码已有 求高人给中文注释(结构分析)代码太长 , 怕吞秒表java程序代码了 。。。
public class TestTimer extends JFrame implements ActionListener, Runnable {
private static TestTimer obj; // 自己的一个静态实例,在这里没什么特别的意思
private JButton btnStart;// 开始按钮
private JButton btnPause;// 暂停按钮
private JButton btnResume;// 恢复按钮
private JButton btnStop;// 停止按钮
private JLabel lblTime;// 显示时间的Label(中文是标签秒表java程序代码?)
private static Thread th;// 一个用来控制时间的线程
private long count;// 计数
public TestTimer(){
super("秒表");// TestTimer继承JFrame , 这里调用父类的构造方法 , 传入的参数表示窗口的标题
btnStart = new JButton("开始");// 初始化按钮,传入的参数表示按钮上显示的文字
btnPause = new JButton("暂停");// 同上
btnResume = new JButton("继续");// 同上
btnStop = new JButton("停止");// 同上
lblTime = new JLabel("00:00:00.000"); // 初始化Label,传入的参数表示Label上显示的文字
this.setLayout(new FlowLayout());// 设置layout风格为FlowLayout(就是设置控件的摆放方式)
this.add(btnStart);// 将控件加入到窗口中
this.add(btnPause);// 同上
this.add(btnResume);// 同上
this.add(btnStop);// 同上
this.add(lblTime);// 同上
btnStart.addActionListener(this);// 为按钮添加监听器(为什么是this,因为TestTimer类实现秒表java程序代码了ActionListener接口,所以可以这样用)
btnPause.addActionListener(this);// 为按钮添加监听器(但我不建议这样,这样的话类的职责不明确)
btnResume.addActionListener(this);// 为按钮添加监听器(当然,如果只是实现需求,怕麻烦可以这么做)
btnStop.addActionListener(this);// 为按钮添加监听器
this.setSize(150, 200);// 设置窗口大小
this.setVisible(true);// 显示窗口
}
public static void main(String[] args) {
obj = new TestTimer();// 主函数入口,初始化实例(其实就是启动窗口)
}
public void actionPerformed(ActionEvent e) {// 这里是实现ActionListener接口的地方
JButton btn = (JButton)e.getSource(); // 获得是哪个按钮触发了事件
if(btn.getText().equals("开始")){// 如果是开始按钮
th = new Thread(obj);// 初始化一个线程(传入obj是因为,TestTimer类实现了Runnable接口,同样我不建议这样做)
count = 0;// count计数器清零
th.start();// 线程启动
}
else if(btn.getText().equals("暂停")){ // 如果是暂停按钮
th.suspend();// 线程挂起(这个方法已经被新版本的JDK遗弃,秒表java程序代码你可以用,但不推荐用)
}
else if(btn.getText().equals("继续")){ // 如果是继续按钮
th.resume();// 线程恢复(同上)
}
else if(btn.getText().equals("停止")){ // 如果是停止按钮
th.stop();// 线程停止(同上)
}
}
@Override
public void run() {// 实现Runnable接口的地方
while(true){// 无限循环(线程一直运行着记录时间)
int ms, seconds, minutes, hours; // 下面一整段都是根据count这个计数器来计算时间
// 秒表java程序代码你看到最后有一个Thread.sleep(1)表示该线程每毫秒工作一次,起到计数的作用)
String msg = "";// msg表示Label上显示的时间
hours = (int)(count / 3600000);
minutes = (int)((count - hours * 3600000) / 60000);
seconds = (int)((count - hours * 3600000 - minutes * 60000) / 1000);
ms = (int)(count % 1000);
if(hours10){// 下面这一串是用来做msg的格式
msg += "0" + hours + ":";
}

推荐阅读