/**
* File name: scrollframe. Java
* Environment: GNU/Linux Ubuntu 7.04 + eclipse 3.2 + JDK 1.6
* Function: Scroll text display panel demo
* Version: 0.0.2.0
* Version change: the source code in version 0.0.1.0 is simplified, and the bug that text cannot be displayed at the beginning is fixed.
* Authors: 88250
* Date: 2007.5.3
* E-mail & MDN: DL88250@gmail.com
* QQ: 845765
*/
Import java. AWT .*;
Import javax. Swing .*;
Public class scrollframe extends jframe
{
Public static string [] textcontent = {"this", "is", "A", "simple", "Demo! "};
Public scrollframe ()
{
Super ("scroll text display by 88250 ");
Scrollpane news = new scrollpane ();
Setbounds (600,300,370,200 );
Setresizable (false );
Setdefaclocloseoperation (jframe. exit_on_close );
Jpanel pane = new jpanel ();
Pane. setlayout (New gridlayout (1, 1, 0, 0 ));
Pane. Add (News );
Setcontentpane (PANE );
Setvisible (true );
}
Public static void main (string [] arguments)
{
Scrollframe = new scrollframe ();
}
}
Class scrollpane extends jpanel implements runnable
{
/**
* Control the animation effect by referencing the ordinate coordinates
*/
Int y;
Public scrollpane ()
{
New thread (this). Start ();
}
/*
* In the run of the thread, it changes y every time and then calls the repaint () method. This method calls the paint method and then draws it again.
* When I draw another image, the Y coordinate in the image changes every time, which forms an animation effect.
*/
Public void run ()
{
While (true)
{
Y-= 1;
If (Y <0)
{
Y = 201;
}
Try
{
Thread. Sleep (20 );
}
Catch (interruptedexception E)
{
}
Repaint ();
}
}
Public void paint (Graphics comp)
{
Graphics2d com2d = (graphics2d) comp;
Font type = new font ("monospaced", Font. Bold, 14 );
Com2d. setfont (type );
Com2d. setcolor (getbackground ());
Com2d. fillrect (0, 0, getsize (). Width, getsize (). Height );
Com2d. setcolor (color. Black );
For (INT I = 0; I <scrollframe. textcontent. length; I ++)
{
Com2d. drawstring (scrollframe. textcontent [I], 5, Y + (20 * I ));
}
}
}