Share on Google+Share on Google+

Move text on the frame

Advertisement
In this section, you will learn how to move text on the frame.

Move text on the frame using Java Program

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(100this);
    timer.start();
  }
   public void actionPerformed(ActionEvent e)  {
    String oldText = label.getText();
    String newText = oldText.substring(1+ oldText.substring(01);
     label.setTextnewText );
  }
   public static void main(String[] args)  {
    MovingText frame = new MovingText();
    frame.setDefaultCloseOperationEXIT_ON_CLOSE );
    frame.setSize(300,100);
    frame.setVisible(true);
  }
}

Advertisements

Posted on: October 30, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.