⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timecounter.java

📁 java编程开发技巧与实例的编译测试通过的所有例程
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class TimeCounter implements Runnable
{
	private ActionListener listener	=	null;
	private Object object			=	null;
	private Thread thread			=	null;
	private int timer	=	0;
	private int time	=	5;
	
	public TimeCounter(ActionListener _listener, Object _obj, int _time)
	{
		time	=	_time;
		object	=	_obj;
		listener	=	AWTEventMulticaster.add(listener, _listener);
	}
	public void start()
	{
		if (thread == null)
		{
			thread = new Thread(this);
			thread.start();
		}
	}
	public void stop()
	{
		thread = null;
	}
	public void run()
	{
		Thread currentThread	=	Thread.currentThread();
		while ((currentThread == thread) && (timer < time * 1000))
		{
			timer	+=	100;
			try
			{	thread.sleep(100);	}
			catch (Exception e)
			{	System.err.println("Unknown error " + e);	}
			if (timer >= time * 1000)
			{
				ActionEvent e = new ActionEvent(object, ActionEvent.ACTION_PERFORMED, "TIME UP");
				listener.actionPerformed(e);
			}
		}
	}
}

⌨️ 快捷键说明

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