当前位置: 代码迷 >> Java相关 >> 跑马灯的问题
  详细解决方案

跑马灯的问题

热度:325   发布时间:2006-03-16 22:59:00.0
跑马灯的问题

我写了二个跑马灯的程序,代码如下
第一个:
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();
}
}
}
}
请大家先运行代码
我感觉第二个有些问题,总觉得空格没有控制好,请大家帮忙改正一下代码,谢谢!

搜索更多相关的解决方案: 当当  public  import  跑马灯  

----------------解决方案--------------------------------------------------------

import javax.swing.*;
import java.awt.*;

public class FontTest extends JApplet implements Runnable
{
String str;
JLabel lblstr;
Font f;
Thread T;
public void init()
{
this.getContentPane().setLayout(new BorderLayout());
str = "锄禾日当午";
lblstr = new JLabel();
this.getContentPane().add(lblstr,BorderLayout.NORTH);
f = new Font("楷体_GB2312", Font.PLAIN, 36);
lblstr.setFont(f);
T = new Thread(this);
T.start();
}

public void run()
{
while(true)
{

for(int i = 0; i < str.length(); i++)
{String strTemp1 = "";

for(int j = 0; j < i; j++)
{
strTemp1 += " ";
}

try
{
T.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
String strTemp2 =strTemp1 + str.charAt(i);
lblstr.setText(strTemp2);
lblstr.validate();
}
}
}
}
你的程序有几个要注意的问题
1,能用局部变量的尽量用局部变量,这样程序会快一些。
2,你的循环弄错了,应把strTemp1在每次进入循环增加空格的时候,都要规零,
否则的话就会出现你那种到后来距离越来越大的感觉
3,你要设置两个空格才能有一个中文字的大小,记住,空格的大小只有一个英文字母那么大。
4,窗口布局要设成BorderLayout才看得更明显一些,因为FlowLayout是居中排列的,也就是说把你的字符串按长度一分为二,一半在左,一半在右,这想必也不是你想看到的结果吧。
5,能简化代码就尽量简化代码,像你原来的代码中的
if(i == 0)
strTemp1 = "";
else
{
for(int j = 0; j < str.length()-(str.length()-i); j++)
{
strTemp1 += " ";
}
}
就是一个多余,没有必要先判断一次i是否等于0;而是直接写for就可以了吗,让for去判断岂不是更好,还有str.length()-(str.length()-i); 是什么东西,这不可以化简成i 吗,岂不是更好看
for(int j = 0; j < i; j++)
{
strTemp1 += " ";
}
是不是好看多了,也快捷多了


----------------解决方案--------------------------------------------------------
谢谢指导,已经明白了。
----------------解决方案--------------------------------------------------------
  相关解决方案