无法获得跑马灯效果
可能的重复:
Java Swing 中的选取框效果
我正在尝试获得选框效果(与我们在html)。但我无法使用这段代码来做到这一点。如何改进这段代码以获得跑马灯效果?
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class tester {
JLabel l;
tester() {
JFrame fr=new JFrame();
JPanel p=new JPanel();
l=new JLabel("");
fr.add(p);
p.add(l);
fr.setVisible(true);
fr.setSize(400,400);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void MarqueeEffect() {
ActionListener ac = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
l.setText("To action alone hast thou a right and never at all to its fruits let not the fruits of action be thy motive; neither let there be in thee any attachment to inaction");
}
};
new Timer(2000,ac).start();
}
public static void main(String args[]) {
tester t=new tester();
t.MarqueeEffect();
}
}
Possible Duplicate:
Marquee effect in Java Swing
I am trying to obtain marquee effect (the same we have in html). But I am unable to do so with this code. How can I improve this code to obtain marquee effect?
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class tester {
JLabel l;
tester() {
JFrame fr=new JFrame();
JPanel p=new JPanel();
l=new JLabel("");
fr.add(p);
p.add(l);
fr.setVisible(true);
fr.setSize(400,400);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void MarqueeEffect() {
ActionListener ac = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
l.setText("To action alone hast thou a right and never at all to its fruits let not the fruits of action be thy motive; neither let there be in thee any attachment to inaction");
}
};
new Timer(2000,ac).start();
}
public static void main(String args[]) {
tester t=new tester();
t.MarqueeEffect();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须扩展
JLabel
并重写paintComponent
才能带来选取框效果。如果不扩展 JLabel,仅仅设置文本是不可能实现的。您可以在自定义的 JLabel 类中执行类似的操作。You will have to extend
JLabel
and overridepaintComponent
to bring marquee effect. It wont come just by setting text to it without extending JLabel. You can do something like this in your customized JLabel class.