[说明]
下面是一个Appkt程序,其功能是从3~100之间(包括3和100)每隔0.5秒显示一个新的数字,如果数字为素数,则显示为灰色,其他为绿色。
程序运行结果如图4所示。
import java. awt. *
import java. applet. Applet
< applet code = ex2_7, class width = 800 height = 400 >
</applet >

public class ex2_7 extends Applet
public Color color2_7 = Color. black;
private iht n2_7 = 3;
public myPrime thPrime2_7;
public void init( )
thPrime2_7 = new myPrime(this);
thPrime2_7, start( );
public void paint(Graphics g)
g, setColor( color2_7 );
g. drawString( (1) ,50,50);
public int getlnt( )
return n2_7;
public void setlnt (int i)
n2_7 = i;
class myPrime extends Thread I
ex2_7 obj2_7;
myPrime (ex2_7 o)
this. obj2_7 = o;
public boolean isPrime(int n)
boolean bPrime = true;
iht i =2;
if( (2) )
return false;
while( i < n - ]&&bPrime)
if( (3) )
bPrime = false;
i++;
return bPrime;
public void run( )
int i;
for (i = 3; (4) ;i++)
if (isPrime(i) )
obj2 _7, color2_7 = Color, gray;
else
obj2_7, color2_7 = Color. green;
(5) ;
obj2_7, repaint( );
try
sleep(S00);
catch (InterruptedException ie)
ex2_7, html
< HTML >
< HEAD >
<TITLE > ex2_7 </TITLE >
</HEAD >
< BODY >
<appletcode =" ex2_, class" width =800 height =400 >
</applet >
</BODY >
</HTML >