home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Web Programming with Visual J++
/
Web_Programming_with_Visual_J_SAMS.NET_Version_1.0_1997.iso
/
source
/
chap12
/
ex12c.java
< prev
next >
Wrap
Text File
|
1996-09-22
|
3KB
|
122 lines
import java.applet.*;
import java.awt.*;
public class EX12C extends Applet implements Runnable
{
protected Thread CounterThread;
protected Button SuspendButton = new Button("Suspend");
protected Button ResumeButton = new Button("Resume");
protected Label CountLabel;
protected TextField MaxCountTextField;
protected int maxCount = 10;
protected int count = maxCount;
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);
// let the user set the max count
p = new Panel();
p.add(new Label("Enter the starting point:"));
MaxCountTextField = new TextField(Integer.toString(maxCount), 3);
p.add(MaxCountTextField);
add("South", 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;
}
else if (evt.target == MaxCountTextField) {
SetMaxCount();
retval = true;
}
return retval;
}
public void run()
{
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
CountDown();
}
}
protected synchronized void CountDown()
{
count--;
CountLabel.setText(Integer.toString(count));
if (count == 0)
// reset to maxCount + 1 since the first call will
// move it back down to maxCount and then display
count = maxCount + 1;
}
protected synchronized void SetMaxCount()
{
try {
maxCount = Integer.parseInt(MaxCountTextField.getText());
count = maxCount + 1;
}
catch (NumberFormatException e)
{
// reset the contents back to the current max
MaxCountTextField.setText(Integer.toString(maxCount));
}
}
}