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

📄 timedcounter.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.*;

public class TimedCounter
    implements Runnable
{
   private ActionListener listener = null;
   private Object object = null;
   private Thread thread = null;
   private int timer = 0;
   private int time = 5;

   public TimedCounter(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 + -