/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package scrollingtextanimation;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Rectangle;
public class ScrollingTextAnimation {
public static void main(String[] args) {
JFrame frame = new JFrame("Scrolling Text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
JPanel panel = new JPanel(null); // Use null layout for manual positioning
frame.add(panel);
JLabel scrollingLabel = new JLabel("This is some scrolling text for animation!");
scrollingLabel.setBounds(frame.getWidth(), 50, 300, 20); // Initial position off-screen right
panel.add(scrollingLabel);
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle bounds = scrollingLabel.getBounds();
bounds.x -= 2; // Move left by 2 pixels
scrollingLabel.setBounds(bounds);
// Reset position if text goes off-screen left
if (bounds.x + bounds.width < 0) {
bounds.x = frame.getWidth();
scrollingLabel.setBounds(bounds);
}
}
});
timer.start();
frame.setVisible(true);
}
}
Tidak ada komentar:
Posting Komentar