stopwatch.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 39 行

JAVA
39
字号
import javax.swing.*;

public class StopWatch extends JLabel implements Runnable
{
	private Thread thread	=	null;
	private int count		=	0;
	public StopWatch()
	{
		super("Time expired: 0 seconds");
	}
	public void start()
	{
		count	=	0;
		thread	=	new Thread(this);
		thread.start();
	}
	public void stop()
	{
		thread	=	null;
	}
	public void run()
	{
		Thread currentThread	=	Thread.currentThread();
		while (thread == currentThread)
		{
			setText("Expired: " + count + "seconds");
			count ++;
			try
			{
				thread.sleep(1000);
			}
			catch	(InterruptedException ie)
			{
				System.err.println("Error: " + ie);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?