目的是在一个窗口中实现一段文字的滚动。目前的做法是这样的:
public void run() {
while (true) {
try {
mf.jl.setText(text.substring(i++, i + 20));//mf:a JFrame;jl: a JLabel;text:the String to roll
} catch (StringIndexOutOfBoundsException se) {
break;
}
try {
Thread.sleep(pct);//每字之间的间隔,调整速度用
} catch (InterruptedException e) {
break;
} }
}这样也可以滚动jl(JLabel)中的文字,但是是一个字一个字的“跳动”,要实现像网页中的Marquee类似的平滑滚动是不是应该用drawString?怎么在这里应用?十分乘五的感谢~~
public void run() {
while (true) {
try {
mf.jl.setText(text.substring(i++, i + 20));//mf:a JFrame;jl: a JLabel;text:the String to roll
} catch (StringIndexOutOfBoundsException se) {
break;
}
try {
Thread.sleep(pct);//每字之间的间隔,调整速度用
} catch (InterruptedException e) {
break;
} }
}这样也可以滚动jl(JLabel)中的文字,但是是一个字一个字的“跳动”,要实现像网页中的Marquee类似的平滑滚动是不是应该用drawString?怎么在这里应用?十分乘五的感谢~~
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;/**
* Test
*/
public class Test extends JFrame { private static final long serialVersionUID = -2397593626990759111L; private JPanel pane = null; private MoveLabel label = null; public Test() {
super("Test");
pane = new JPanel();
label = new MoveLabel("带有滚动效果的标签");
pane.add(label);
this.getContentPane().add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
} public static void main(String args[]) {
new Test();
} /**
* 带有滚动效果的Label标签
*/
private class MoveLabel extends JLabel implements Runnable { private static final long serialVersionUID = 1891684760189602720L; private String text = null; private Thread thread = null; private int x = 0; private int w = 0, h = 0; public MoveLabel(String text) {
super(text);
this.text = text;
thread = new Thread(this);
thread.start();
} public String getText() {
return text;
} public void setText(String text) {
super.setText(text);
this.text = text;
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.getBackground());
g.fillRect(0, 0, w = this.getWidth(), h = this.getHeight());
g.setColor(this.getForeground());
g.setFont(this.getFont());
g.drawString(text, x, h - 2);
} public void run() {
while (true) {
x -= 2;
if (x < -w) {
x = w;
}
this.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;import javax.swing.*;
import javax.swing.event.MouseInputListener;public class T {
public static void main(String[] args) {
final JPanel p = new JPanel(new GridLayout(1, 2));
JLabel l1 = new JLabel("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
JLabel l2 = new JLabel("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
p.add(l1);
p.add(l2);
final JViewport vp = new JViewport() {
public void doLayout() {
p.setPreferredSize(new Dimension(getWidth() * 2, getHeight()));
super.doLayout();
}
};
vp.setView(p);
Timer timer = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Point pt = vp.getViewPosition();
if (pt.x == vp.getWidth()) {
pt.x = 0;
}
else {
pt.x++;
}
vp.setViewPosition(pt);
}
});
timer.start();
JFrame f = new JFrame();
f.getContentPane().add(vp, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
也谢谢gtlang78,
高手真多啊。