编译后,出现14个错误,我修改了7个,还有7个不明白是什么原因,想高手给分析一下。
其实我是按书来打出来的
其运行是在HTML上加上<applet code = .....>,然后通过HTML页面打开来看的
但是在编译的时候,却出现错误,我不明白个中道理,想请高手给分析一下,为什么会出现这样的错误。
程序如下:
import java.applet.Applet.*;
import java.awt.*;
public class TextRunnable extends Applet implements Runnable
{
Label prompt1 = new Label("子线程1");
Label prompt2 = new Label("子线程2");
TextField threadfirst = new TextField(25);
TextField threadsecond = new TextField(25);
Thread thread1,thread2;
int count1;
int count2;
public void init()
{
add(prompt1);
add(prompt2);
add(threadfirst);
add(threadsecond);
}
public void start()
{
Thread thread1 = new Thread(this,"FirstThread");
Thread thread2 = new Thread(this,"SecondThread");
thread1.start();
thread2.start();
}
public void run()
{
String currentRunning;
while (true)
{
try
{
Thread.sleep((int)(Math.random()*100));
}
catch (InterruptedException e)
{
}
currentRunning = Thread.currentThread().getName();
if (currentRunning.equals("FirstThread"))
{
count1++;
threadfirst.setText("线程1第"+count1+"次被调度");
}
else if (currentRunning.equals("SecondThread"))
{
count2++;
threadsecond.setText("线程2第"+count2+"次被调度");
}
}
}
}