home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-22 | 1.8 KB | 94 lines |
- import java.applet.*;
- import java.awt.*;
-
- public class EX12B extends Applet implements Runnable
- {
- protected Thread CounterThread;
- protected Button SuspendButton = new Button("Suspend");
- protected Button ResumeButton = new Button("Resume");
- protected Label CountLabel;
- protected int count = 10;
-
- public void init()
- {
- // gives us some control over where the controls are
- // to be placed
- setLayout(new BorderLayout());
-
- // provide some control over the count
- Panel p = new Panel();
- p.add(SuspendButton);
- p.add(ResumeButton);
- add("North", p);
-
- // provide a visual of the count
- p = new Panel();
- p.add(new Label("Count:"));
- CountLabel = new Label(Integer.toString(count));
- p.add(CountLabel);
- add("Center", p);
- }
-
- public void start()
- {
- if (CounterThread == null) {
- // allocate the thread, using the local run method
- CounterThread = new Thread(this);
- CounterThread.start();
- }
- }
-
- public void stop()
- {
- if (CounterThread != null) {
- // stop the thread
- CounterThread.stop();
- CounterThread = null;
- }
- }
-
- public boolean action(Event evt, Object arg)
- {
- boolean retval = false; // assume event not processed
-
- if (evt.target == SuspendButton) {
- if (CounterThread != null)
- CounterThread.suspend();
-
- retval = true;
- }
- else if (evt.target == ResumeButton) {
- if (CounterThread != null)
- CounterThread.resume();
-
- retval = true;
- }
-
- return retval;
- }
-
- public void run()
- {
- while (true) {
- try {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {}
-
- CountDown();
- }
- }
-
- protected void CountDown()
- {
- count--;
-
- CountLabel.setText(Integer.toString(count));
-
- if (count == 0)
- // reset to 11 since the first call will move it back
- // down to 10 and then display
- count = 11;
- }
- }
-