我写了二个跑马灯的程序,代码如下
第一个:
import java.awt.event.*;
import javax.swing.*;
public class PmdLabel extends JApplet implements Runnable
{
String strLabel, strPmd, strPmdTemp;
Thread T;
public void init()
{
strLabel = new String("☆欢迎光临当当的小站☆");
strPmdTemp = "";
T = new Thread(this);
T.start();
}
public void run()
{
while(true)
{
strPmd = "";
for(int i = 0; i < strLabel.length(); i++)
{
this.showStatus(strPmd);
strPmd += strLabel.charAt(i);
try
{
T.sleep(500);
}
catch(Exception e)
{
e.printStackTrace();
}
}
for(int i = 0; i < strPmd.length(); i++)
{
strPmdTemp = strPmd.substring(i, strPmd.length());
this.showStatus(strPmdTemp);
try
{
T.sleep(500);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
第二个:
import javax.swing.*;
import java.awt.*;
public class FontTest extends JApplet implements Runnable
{
String str, strTemp1, strTemp2;
JLabel lblstr;
Font f;
Thread T;
public void init()
{
this.getContentPane().setLayout(new FlowLayout());
str = "锄禾日当午";
lblstr = new JLabel();
this.getContentPane().add(lblstr);
f = new Font("楷体_GB2312", Font.PLAIN, 36);
T = new Thread(this);
T.start();
}
public void run()
{
while(true)
{
strTemp1 = "";
for(int i = 0; i < str.length(); i++)
{
if(i == 0)
strTemp1 = "";
else
{
for(int j = 0; j < str.length()-(str.length()-i); j++)
{
strTemp1 += " ";
}
}
try
{
T.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
strTemp2 =strTemp1 + str.charAt(i);
lblstr.setFont(f);
lblstr.setText(strTemp2);
lblstr.validate();
}
}
}
}
请大家先运行代码
我感觉第二个有些问题,总觉得空格没有控制好,请大家帮忙改正一下代码,谢谢!