In this section, you will learn how to move text on the frame. As you have already learned about the html marquee tag. Here we are going to do the same thing in Swing. We have used Timer class to move the specified string.
|
import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; public class MovingText extends JFrame implements ActionListener{ JLabel label; JLabel label2; public MovingText() { label = new JLabel( "Hello World,Where there is will there is& nbsp;a way"); getContentPane().add(label, BorderLayout.NORTH); javax.swing.Timer timer = new javax.swing.Timer(100, this); timer.start(); } public void actionPerformed(ActionEvent e) { String oldText = label.getText(); String newText = oldText.substring(1) + oldText.substring(0, 1); label.setText( newText ); } public static void main(String[] args) { MovingText frame = new MovingText(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.setSize(300,100); frame.setVisible(true); } } |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.