如何在文本中添加字幕行为JLabel
?
我试过这个
JLabel search = new JLabel("<html><marquee>Search</marquee><html>");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
有关如何执行此操作的详细信息,请参阅http://forums.sun.com/thread.jspa?forumID=57&threadID=605616 :)
(编辑:我可能会直接在paint()方法中使用System.currentTimeMillis()而不是使用计时器,然后除/模(%)它以使其进入示例中“x偏移”所需的范围) 。通过增加除数的大小,您可以更改速度 ((System.currentTimeMillis() / 200) % 50)。
(编辑 2:我刚刚更新了下面的代码来解决重绘问题。现在我们在 Paint 方法中安排重绘;也许有更好的方法,但这有效:))
(编辑3:呃,我尝试使用更长的字符串,但它搞砸了。这很容易修复(再次按宽度增加范围以补偿负值,然后减去宽度)
package mt;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JLabel;
public class MyJLabel extends JLabel {
public static final int MARQUEE_SPEED_DIV = 5;
public static final int REPAINT_WITHIN_MS = 5;
/**
*
*/
private static final long serialVersionUID = -7737312573505856484L;
/**
*
*/
public MyJLabel() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param image
* @param horizontalAlignment
*/
public MyJLabel(Icon image, int horizontalAlignment) {
super(image, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param image
*/
public MyJLabel(Icon image) {
super(image);
// TODO Auto-generated constructor stub
}
/**
* @param text
* @param icon
* @param horizontalAlignment
*/
public MyJLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param text
* @param horizontalAlignment
*/
public MyJLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param text
*/
public MyJLabel(String text) {
super(text);
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
protected void paintComponent(Graphics g) {
g.translate((int)((System.currentTimeMillis() / MARQUEE_SPEED_DIV) % (getWidth() * 2)) - getWidth(), 0);
super.paintComponent(g);
repaint(REPAINT_WITHIN_MS);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9171 次 |
最近记录: |