The example of this article tells the LABEL label with rolling effects based on the thread -based thread. Share it for everyone for your reference. The specifics are as follows:
Import java.awt.graphics; Import javax.swing.jframe; Import javax.swing.jlabel; Import javax.swing.jpanel; */Public Class Test EXTENDS Jframe {Private Static Final Long SerialVersionuid = -2397593626990759111L; Private Jpanel Pane = Null; Private Movelabel Label = NULL; Public Test () uper ("test"); Pane = new jpanel (); label = new movelabel ("" with Tags of rolling effects "); Pane.add (label); this.GetContentPane (). Add (phen); this.setDefaultCloseoperation (jframe.exit_on_close); this.Setsize (300, 200). ; this.setvisible (true); } public static void main(String args[]) { new Test(); } /** * 带有滚动效果的Label标签,可继续拓展很多特效,例如颜色变换、速度变换等*/ private class MoveLabel extends JLabel Implements runnable {Private Static Final Long SerialVersionuid = 1891684760189602720L; Private String Text = Null; Private Thread Thread = Null; E int x = 0; Private Int W = 0, H = 0; Public Movelabel (String Text) {Super (Text) ; this.Text = text; thread = new thread (this); thread.start ();} Public String Gettext () {Return TEXT;} Public Voidtext (String TEXT) {s uper.setText (text); this.text = Text;} Protected void Paintcomponent (Graphics G) {Super. Paintcomponent (g); g.SetColor (this.getBackground ()); Width (), h = this. getHeight()); g.setColor(this.getForeground()); g.setFont(this.getFont()); g.drawString(text, x, h - 2); } public void run() { while (true ) {x- = 2; if (x <-w) {x = w;} this.RePaint (); try {thread.sleep (50);} catch (interruptedException E) {e.printstacktrace ();}}} }}}
It is hoped that this article is helpful to everyone's Java program design.