Pages - GamesandApps

Java Code to Create Moving Text

This Program in Java will teach you how to add Border to your java program, how to move your text i.e. Labels or Images along x-axis and y-axis. It will also let you make creative and good looking programs or java applications by creating simple designs and animations. Please feel free to ask anything by commenting under the post.


Compile this code as first.java :-


import java.awt.*;
import javax.swing.*;
import java.awt.event.* ;
import javax.swing.border.* ;

class first extends JFrame implements Runnable
{
int x;
Thread t;
JLabel l,l2,l3;
Font f;

first()
{
l=new JLabel("MANI");
l2=new JLabel("NDER");
l3=new JLabel("MANINDER");
f=new Font("SanSerif",Font.BOLD,25);
Thread t=new Thread(this);
JPanel p = new JPanel();
Border[] border = new Border[]{BorderFactory.createMatteBorder(14, 8, 14, 8, Color.black)};
p.setLayout(new GridLayout(border.length, 0, 3, 3));
for (int i = 0; i < border.length; i++) 
{
JPanel borderPanel = new JPanel();
borderPanel.setBorder(border[i]);
p.add(borderPanel);
}
add(l);
add(l2);
add(l3);

l.setFont(f);
l2.setFont(f);
l3.setFont(f);
getContentPane().add(p, "Center");
t.start();
setTitle("Java Expo");
setSize(800, 600);
setResizable(false);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-getWidth())/2,
(Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run()
{
for(x=30;x<=320;x++)
{
l.setBounds(x,50,150,30);
l.setVisible(true);
try
{
Thread.sleep(10);
}
catch(Exception e)
{}
}
for(x=700;x>=383;x--)
{
l2.setBounds(x,50,150,30);
l2.setVisible(true);
try
{
Thread.sleep(10);
}
catch(Exception e)
{}
}

l.setForeground(Color.red);
l2.setForeground(Color.red);
try
{

Thread.sleep(1000);
}
catch(Exception sdfdsf)
{}
l3.setBounds(320,50,150,30);
for(x=50;x<=350;x++)
{
l3.setBounds(320,x,150,30);
l3.setVisible(true);
try
{
Thread.sleep(15);
}
catch(Exception e)
{}
}
}

public static void main(String[] args) 
{
JFrame frame = new first();
  frame.show();
}
}


Java code to create moving text output
Output of the Moving Text Program in Java
Please provide me your feedback after you review this program.

No comments:

Post a Comment