clockapplet.java

来自「一个十分好的java基础学习的课件」· Java 代码 · 共 86 行

JAVA
86
字号
// ClockApplet.java: Display a running clock on the applet
import java.applet.*;
import java.awt.*;
import java.util.*;

public class ClockApplet extends CurrentTimeApplet
  implements Runnable
{
  // Declare a thread for running the clock
  private Thread thread = null;

  // Determine if the thread is suspended
  private boolean suspended = false;

  // Initialize applet
  public void init()
  {
    super.init();

    // Create the thread
    thread = new Thread(this);

    // Start the thread
    thread.start();
  }

  // Implement the start() method to resume the thread
  public void start()
  {
    resume();
  }

  // Implement the run() method to dictate what the thread will do
  public void run()
  {
    while (true)
    {
      // Repaint the clock to display current time
      stillClock.repaint();

      try
      {
        thread.sleep(1000);
        waitForNotificationToResume();
      }
      catch (InterruptedException ex)
      {
      }
    }
  }

  private synchronized void waitForNotificationToResume()
    throws InterruptedException
  {
    while (suspended)
      wait();
  }

  // Implement the stop method to suspend the thread
  public void stop()
  {
    suspend();
  }

  // Destroy the thread
  public void destroy()
  {
    thread = null;
  }

  // Resume the suspended thread
  public synchronized void resume()
  {
    if (suspended)
    {
      suspended = false;
      notify(); // Notify the waiting object
    }
  }

  // Suspend the thread
  public synchronized void suspend()
  {
    suspended = true;
  }
}

⌨️ 快捷键说明

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