Search This Blog

Java Application Source Code to Scroll Text Along JFrame

This is a simple java program for beginners to show how to scroll text in the JFrame. The program uses an anonymous (unnamed) thread object to move the scroll or move the text. The Thread moves two JLabel objects (containing text) by repeatedly calling setLocation(int x, int y) method of JLabel. The speed of the motion of text in this program can be changed by changing value of i or changing sleep period of the thread.




import javax.swing.JLabel; import javax.swing.WindowConstants; import javax.swing.JFrame; public class NewJFrame extends JFrame{ String top="This is Network Programming Lab"; String bottom="This is also Computer Graphics Lab"; JLabel Jtop=new JLabel(); JLabel Jbottom=new JLabel(); public NewJFrame() { setLayout(null); setSize(400,300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); add(Jtop); Jtop.setLocation(0, 10); Jtop.setSize(getWidth(), 20); Jtop.setText(top); add(Jbottom); Jbottom.setLocation(400, getHeight()-50-15); Jbottom.setSize(getWidth(),20); Jbottom.setText(bottom); t.start(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } Thread t=new Thread() { public void run() { int i=3; while(true) { try { Thread.sleep(80); } catch (Exception ex) {} Jtop.setLocation(Jtop.getX()+i,Jtop.getY()); Jbottom.setLocation(Jbottom.getX()-i,Jbottom.getY()); if(Math.abs(Jtop.getX())>=400) i*=-1; } } }; }

No comments:

Post a Comment