0

Can anybody give me some advice on how can I do that? I found some code on the internet but I don't know how to use it how to add it to my code and use it since that code creates a frame on its own.

How can I assign a JLabel to this code?

Here is the code (I want to use this for a label I have on a frame I created):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
 
public class JScrollingText extends JLabel {
 
    private int speed;
 
    private int period;
 
    private int offset;
 
    private int x;
 
    public JScrollingText(String text) {
        this(text, 1);
    }
 
    public JScrollingText(String text, int speed) {
        this(text, speed, 100);
    }
 
    public JScrollingText(String text, int speed, int period) {
        this(text, speed, period, 0);
    }
 
    public JScrollingText(String text, int speed, int period, int offset) {
        super(text);
        this.speed = speed;
        this.period = period;
        this.offset = offset;
    }
 
    public void paintComponent(Graphics g) {
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        g.setColor(getForeground());
 
        FontMetrics fm = g.getFontMetrics();
        Insets insets = getInsets();
 
        int width = getWidth() - (insets.left + insets.right);
        int height = getHeight() - (insets.top + insets.bottom);
 
        int textWidth = fm.stringWidth(getText());
        if (width < textWidth) {
            width = textWidth + offset;
        }
        x %= width;
 
        int textX = insets.left + x;
        int textY = insets.top + (height - fm.getHeight()) / 2  + fm.getAscent();
 
        g.drawString(getText(), textX, textY);
        g.drawString(getText(), textX + (speed > 0 ? -width : width), textY);
    }
 
    public void start() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                x += speed;
                repaint();
            }
        };
        timer.scheduleAtFixedRate(task, 0, period);
    }
 
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        JButton quit = new JButton("Quitter");
        quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().add(quit);
        JScrollingText scrollingText1 = new JScrollingText("Barre des tâches... Pressez le bouton Quitter", -3);
        scrollingText1.setBorder(BorderFactory.createEtchedBorder());
        scrollingText1.start();
        frame.getContentPane().add(scrollingText1, BorderLayout.NORTH);
        JScrollingText scrollingText2 = new JScrollingText("Barre des tâches... Pressez le bouton Quitter");
        scrollingText2.setBorder(BorderFactory.createEtchedBorder());
        scrollingText2.start();
        scrollingText2.setBackground(Color.YELLOW);
        scrollingText2.setOpaque(true);
        frame.getContentPane().add(scrollingText2, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) Don't extend a JLabel. A JLabel already has custom painting code. Extend either a JComponent or JPanel to add your custom painting code. 2) Don't use an AWT Timer for animation. All Swing components should be updated on the `Event Dispatch Thread (EDT)`. So you should be using the [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for animation. 3) Check out the [Marquee Panel](https://tips4java.wordpress.com/2011/04/24/marquee-panel/) for one approach. – camickr Jun 24 '21 at 14:16

1 Answers1

0

Bienvenu aux les Stacks.

The key lines are

       frame.getContentPane().add(scrollingText1, BorderLayout.NORTH);

       frame.getContentPane().add(scrollingText2, BorderLayout.SOUTH);

You will have to do a similar .add to an appropriate container or component in your code.

You will probably have to create an instance of JScrollPane or some kind of Pane, add that to your GUI, and add the ScrollingText(s) to that.

Also, I think it better to not use the J initial for the scrolling text class. That will help you (and other coders?) avoid thinking it is part of the Java libraries. Call the class ScrollingText or RKScrollingText or similar.

Alan
  • 716
  • 5
  • 15