在java的jLabel怎么实现字体流动

 我来答
jimlaren
2011-09-02 · TA获得超过400个赞
知道小有建树答主
回答量:253
采纳率:0%
帮助的人:194万
展开全部
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class JMarqueeLabel extends JPanel implements Runnable
{
/**
*
*/
private static final long serialVersionUID = -2973353417536204185L;
private int x;
private FontMetrics fontMetrics;
public static final int MAX_SPEED = 30;
public static final int MIN_SPEED = 1;
private int speed;
public static final int SCROLL_TO_LEFT = 0;
public static final int SCROLL_TO_RIGHT = 1;
private int scrollDirection = 0;
private boolean started = false;
private JLabel label;

public JMarqueeLabel(String text)
{
super();
label = new JLabel(text)
{
/**
*
*/
private static final long serialVersionUID = -870580607070467359L;

@Override
protected void paintComponent(Graphics g)
{
g.translate(x, 0);
super.paintComponent(g);
}
};
setLayout(null);
add(label);
setSpeed(10);
setScrollDirection(SCROLL_TO_RIGHT);
}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
label.paintComponents(g);
}

public void setScrollDirection(int scrollDirection)
{
this.scrollDirection = scrollDirection;
}

public int getScrollDirection()
{
return scrollDirection;
}

public void setSpeed(int speed)
{
if (speed < MIN_SPEED || speed > MAX_SPEED)
{
throw new IllegalArgumentException("speed out of range");
}
this.speed = speed;
}

public int getSpeed()
{
return speed;
}

@Override
public void validateTree()
{
System.out.println("Validate...");
super.validateTree();
label.setBounds(0, 0, 2000, getHeight());
if (!started)
{
x = getWidth() + 10;
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
started = true;
}
}

public String getText()
{
return label.getText();
}

public void setText(String text)
{
label.setText(text);
}

public void setTextFont(Font font)
{
label.setFont(font);
fontMetrics = label.getFontMetrics(label.getFont());
}

@Override
public void run()
{
fontMetrics = label.getFontMetrics(label.getFont());
try
{
Thread.sleep(100);
} catch (Exception e)
{
}
while (true)
{
if (scrollDirection == SCROLL_TO_LEFT)
{
x--;
if (x < -fontMetrics.stringWidth(label.getText()) - 10)
{
x = getWidth() + 10;
}
}
if (scrollDirection == SCROLL_TO_RIGHT)
{
x++;
if (x > getWidth() + 10)
{
x = -fontMetrics.stringWidth(label.getText()) - 10;
}
}
repaint();
try
{
Thread.sleep(35 - speed);
} catch (Exception e)
{
}
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式