Java Multithreaded Experiment Scrolling word

Source: Internet
Author: User

Package Com.rgy.test;import Java.awt.gridlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JLabel; Import Javax.swing.jpanel;import Javax.swing.jtextfield;import Javax.swing.windowconstants;public class Test3 Extends jframe{//add attribute//private JPanel panel[];p rivate jtextfield tf[];p rivate JLabel label_sleep[];p rivate JTextField Tf_sleep[];p rivate JButton button_start[];p rivate JButton button_interrupt[];p rivate JLabel label_state[]; Private JTextField tf_state[];p rivate mythread_0 thread_0;private mythread_1 thread_1;private MyThread_2 thread_2;// Private String texts[]={"Welcome", "Hello", "Rolly"};//private int sleeptime0;private int sleeptime1;private int Sleeptime2;public Test3 (String str) {super (str); this.setdefaultcloseoperation (Windowconstants.exit_on_close); This.setbounds (500, 250, 500, 320);//Add function code this.setlayout (new GridLayout (6,1)); Tf=new jtextfield[3];p anel=new JPanel [3];label_sleep=new jlabel[3];tf_sleep=new jtextfield[3];button_start=new jbutton[3];button_interrupt=new JButton[3];label_state= New jlabel[3];tf_state=new jtextfield[3];//for (int i=0;i<3;i++) {panel[i]=new JPanel (); Tf[i]=new JTextField ( String.Format ("%160s", Texts[i]); Tf[i].sethorizontalalignment (jtextfield.right);//label_sleep[i]=new JLabel (" JTextField ("Tf_sleep[i]=new"), Button_start[i]=new JButton ("Start"), Button_interrupt[i]=new JButton ("interruption"); Label_state[i]=new JLabel ("state"); tf_state[i]=new JTextField (n); tf_state[i].seteditable (false);//panel[i].add ( Label_sleep[i]);p Anel[i].add (Tf_sleep[i]);p Anel[i].add (Button_start[i]);p Anel[i].add (Button_interrupt[i]); Panel[i].add (Label_state[i]);p Anel[i].add (Tf_state[i]); Button_start[i].addactionlistener (new Button_handler ()); Button_interrupt[i].addactionlistener (New Button_handler ()); This.add (Tf[i]); This.add (Panel[i]);} This.pack (); this.setvisible (true);} Class Mythread_0 extends Thread{public void run () {while (true) {try {String str=tf[0].gettext (); Tf[0].settext (str.substring (1) +str.substring (0,1)); sleep (SLEEPTIME0);} catch (Exception e) {break;}}}} Class Mythread_1 extends Thread{public void run () {while (true) {try {String str=tf[1].gettext (); Tf[1].settext ( Str.substring (1) +str.substring (0,1)); sleep (sleeptime1);} catch (Exception e) {break;}}}} Class Mythread_2 extends Thread{public void run () {while (true) {try {String str=tf[2].gettext (); Tf[2].settext ( Str.substring (1) +str.substring (0,1)); sleep (sleeptime2);} catch (Exception e) {break;}}}} Class Button_handler implements Actionlistener{public void actionperformed (ActionEvent e) {if (E.getsource () ==button_ Start[0]) {Sleeptime0=integer.parseint (Tf_sleep[0].gettext ()); thread_0=new mythread_0 (); Thread_0.start (); button_ Start[0].setenabled (false); button_interrupt[0].setenabled (true); Tf_state[0].settext ("" +thread_0.getstate ());} if (E.getsource () ==button_interrupt[0]) {thread_0.interrupt (); button_start[0].setenabled (true); button_interrupt[ 0].setenabled (false); Tf_state[0].settext ("" +thread_0.getstaTe ());} if (E.getsource () ==button_start[1]) {Sleeptime1=integer.parseint (Tf_sleep[1].gettext ()); Thread_1=new MyThread_1 () ; Thread_1.start (); button_start[1].setenabled (false); button_interrupt[1].setenabled (true); Tf_state[1].settext (" "+thread_1.getstate ());} if (E.getsource () ==button_interrupt[1]) {thread_1.interrupt (); button_start[1].setenabled (true); button_interrupt[ 1].setenabled (false); Tf_state[1].settext ("" +thread_1.getstate ());} if (E.getsource () ==button_start[2]) {Sleeptime2=integer.parseint (Tf_sleep[2].gettext ()); Thread_2=new MyThread_2 () ; Thread_2.start (); button_start[2].setenabled (false); button_interrupt[2].setenabled (true); Tf_state[2].settext (" "+thread_2.getstate ());} if (E.getsource () ==button_interrupt[2]) {thread_2.interrupt (); button_start[2].setenabled (true); button_interrupt[ 2].setenabled (false); Tf_state[2].settext ("" +thread_2.getstate ());}}} public static void Main (String args[]) {new Test3 ("scrolling word");}}

Java Multithreaded Experiment Scrolling word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.