import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class appletEx001 extends JApplet
{
public void init()
{
Container cp=getContentPane();
CBox pa=new CBox();
pa.setBackground(Color.black);
cp.add(pa,BorderLayout.CENTER);
}
}
class CBox extends JPanel implements Runnable
{
int x,y;
String Message="Java Now!";
Font f=new Font("TimesRoman",Font.BOLD,24);
Thread th1=null;
public CBox()
{
th1=new Thread(this);
th1.start();
}
public void run()
{
x=getWidth();
y=getHeight()/2;
while(true)
{
x=x-5;
if(x==0)
x=getSize().width;//什么意思?
repaint();
try{
th1.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.RED);
super.paintComponent(g);
g2.setFont(f);
g2.drawString(Message,x,y);
}
}
在执行的时候并为跳出预期的滚动字条的效果,而是一个只有背景色的APPLET程序。
因为刚接触线程,不知道哪里错了,请各位不吝指教,谢谢!!