开发者

unable to get marquee effect [duplicate]

开发者 https://www.devze.com 2023-02-27 10:35 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Marquee effect in Java Swing
This question already has answers here: Closed 11 years ago.

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.setDef开发者_如何学运维aultCloseOperation(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();
 }
}


You will have to extend JLabel and override paintComponent 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.

protected void paintComponent(Graphics g)
{         
g.translate((int)((System.currentTimeMillis() / MARQUEE_SPEED_DIV) % (getWidth() * 2)) - getWidth(), 0);         
super.paintComponent(g);        
repaint(REPAINT_WITHIN_MS);     
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号