japplet中如何擦除字符串?

来源:百度知道 编辑:UC知道 时间:2024/06/05 21:29:43
目标是实现:编写一个APPLET程序,在里面显示一行标题"WELCOME TO CHINA"要求象打字一样一个字母一个字母的显示,显示完再全部隐去,重复显示.
我的程序如下:
import javax.swing.JApplet;
import java.awt.Graphics;
public class Welcome extends JApplet{
public void paint(Graphics g){
String text="WELCOME TO CHINA";
String buff="";
while(true){
for(int i=0;i<=buff.length();i++){
buff=text.substring(0,i+1);
try{
Thread.sleep(100);
}catch(InterruptedException e){}
g.drawString(buff,30,50);
}

//g.update();
//g.clearRect(0,0,300,400);
//repaint();
//super.paint(g);
}
}
}
倒是能实现打字效果,但是不直到如何擦除以显示的字符串,所以只能显示一遍不能隐去,重复显示。我也试着用了一个Label,利用它的setText()方法来实现,可是只能
显示为最后设置的那个字符串,好像是在后台运行完之后才放到网页中显示的似的,请教应该怎么写呢?
楼下说的也不对,加上this.setVisible(false) ; 这一句没有什么用。

import javax.swing.JApplet;
import java.awt.Graphics;
public class Welcome extends JApplet{
public void paint(Graphics g){
String text="WELCOME TO CHINA";
String buff="";
while (true) {
this.show() ;
for(int i=0;i<text.length();i++)
{
buff=text.substring(0,i+1);
try{
Thread.sleep(100);
}catch(InterruptedException e){}
g.drawString(buff, 30, 50) ;
}
try{
Thread.sleep(200);
}catch(InterruptedException e){}
this.setVisible(false) ;
}
}
}

楼主试试吧