还是看我的吧:////////////////////////ScrolledString.java import java.awt.*;public class ScrolledString {
/** 字符串默认字体 */ public static final Font DEFAULT_FONT = new Font("simsun", Font.BOLD, 12);
/** 字符串默认颜色 */ public static final Color DEFAUTE_COLOR = Color.black;
/** 字符串字体 */ private Font font;
/** 字符串颜色 */ private Color color;
/** 字符串 */ private String info;
/** 显示的水平位置 */ private int x; /** 显示的垂直位置 */ private int y; public ScrolledString(String s, int x, int y) { this(s, x, y, null, null); } public ScrolledString(String s, int x, int y, Font f, Color c) { this.info = s; this.x = x; this.y = y; setFont(f); setColor(c); } public Point getLocation() { return new Point(x, y); }
设置多个Label,用timer计时,然后移动位置
你的,估计再加上改变字体大小就行了不过,这种方法很耗资源
希望你能找出更好的办法
这样应该就可以搞定了
http://blog.csdn.net/chenweionline/archive/2008/08/18/2790322.aspx
import java.awt.*;public class ScrolledString {
/** 字符串默认字体 */
public static final Font DEFAULT_FONT = new Font("simsun", Font.BOLD, 12);
/** 字符串默认颜色 */
public static final Color DEFAUTE_COLOR = Color.black;
/** 字符串字体 */
private Font font;
/** 字符串颜色 */
private Color color;
/** 字符串 */
private String info;
/** 显示的水平位置 */
private int x; /** 显示的垂直位置 */
private int y; public ScrolledString(String s, int x, int y) {
this(s, x, y, null, null);
} public ScrolledString(String s, int x, int y, Font f, Color c) {
this.info = s;
this.x = x;
this.y = y;
setFont(f);
setColor(c);
} public Point getLocation() {
return new Point(x, y);
}
public void setLocation(Point p) {
this.x = p.x;
this.y = p.y;
}
public String getString() {
return info;
} public void setColor(Color c) {
this.color = (c == null) ? DEFAUTE_COLOR : c;
} public Font getFont() {
return this.font;
} public void setFont(Font f) {
this.font = (f == null) ? DEFAULT_FONT : f;
} /**
* 绘制此{@code ScrolledString}到指定图形上下文。
*
* @param g 图形上下文
*/
public void draw(Graphics g) {
Color oldColor = g.getColor();
Font oldFont = g.getFont();
g.setColor(color);
g.setFont(font);
g.drawString(info, x, y);
g.setColor(oldColor);
g.setFont(oldFont);
}}////////////////////////ScrolledStringList.java
import java.awt.*;
import java.util.ArrayList;public class ScrolledStringList {
//字符串列表
private ArrayList<ScrolledString> stringList = new ArrayList<ScrolledString>();
//显示范围
private Rectangle rectangle;
//卷动速率,单位:像素/帧
private int speed;
//字符串之间的间隔
private int gap;
//字体大小增量
private int fontSizeInc; public ScrolledStringList(Rectangle r) {
this(r, 10, 2, 100);
}
public ScrolledStringList(Rectangle r, int speed, int fontSizeInc, int gap) {
this.rectangle = r;
this.speed = speed;
this.fontSizeInc = fontSizeInc;
this.gap = gap;
}
public void add(String[] ss) {
for (String s : ss) {
addString(s);
}
}
public void addString(String s) {
synchronized (stringList) {
ScrolledString ss = null;
if (!stringList.isEmpty()) {
ScrolledString topString = getTopString();
int topSy = topString.getLocation().y;
ss = new ScrolledString(s, 0, topSy - gap);
} else {
ss = new ScrolledString(s, 0, 0);
}
stringList.add(ss);
}
} private ScrolledString getTopString() {
ScrolledString top = stringList.get(0);
for (ScrolledString ss : stringList) {
if (ss.getLocation().y < top.getLocation().y) {
top = ss;
}
}
return top;
}
public void draw(Graphics g) {
synchronized (stringList) {
for (ScrolledString ss : stringList) {
Point p = ss.getLocation();
Font f = ss.getFont();
p.y += speed;
if (p.y > rectangle.height) {
ss.setFont(ScrolledString.DEFAULT_FONT);
ScrolledString topString = getTopString();
int topSy = topString.getLocation().y;
p.y = (topSy <= 0) ? (topSy - gap) : rectangle.y;
}
if (rectangle.contains(p)) {
float ds = p.y < rectangle.height/2 ? fontSizeInc : -fontSizeInc;
ss.setFont(f.deriveFont(f.getSize() + ds));
}
ss.setLocation(p);
}
for (ScrolledString ss : stringList) {
if (rectangle.contains(ss.getLocation())) {
ss.draw(g);
}
}
}
}
}////////////////////////Painter.java
import java.awt.Graphics;
import java.awt.Rectangle;import javax.swing.JFrame;
import javax.swing.JPanel;public class Painter extends JPanel implements Runnable { private static final long serialVersionUID = -2055656611173452692L;
// 刷新速率,单位:帧/秒
private static final int FPS = 5;
private ScrolledStringList scrolledStringList;
public Painter(ScrolledStringList ssl) {
this.scrolledStringList = ssl;
} public void paintComponent(Graphics g) {
super.paintComponent(g);
scrolledStringList.draw(g);
} public void run() {
int delay = 1000 / FPS;
while (true) {
repaint();
try {
//注意此处并没精确控制刷新率
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(300, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
ScrolledStringList ssl = new ScrolledStringList(new Rectangle(0, 0, 300, 600));
//姓名可以添加任意多个
String[] names = {"姓名1", "姓名2", "姓名3", "姓名4", "姓名5", "姓名6"};
ssl.add(names);
Painter painter = new Painter(ssl);
jf.getContentPane().add(painter);
jf.setVisible(true);
new Thread(painter).start();
}
}