The Java Swing thread performs the task (otherwise the interface is always in a non-clickable state)

Source: Internet
Author: User

/** * uses the thread interrupt application in the swing progress bar to call JProgressBar's SetValue () method in Run (). * <p> * This example applies a thread interrupt, 2 interrupt methods: * <ul> * <li> Apply the Interrupt () method </li> * <li> use an infinite loop in run (), Then use a Bolshoi tag to control the loop stop </li> * </ul> * In addition, there are internal classes and anonymous inner classes used separately.     * * @author HAN * * */@SuppressWarnings ("Serial") public class Threadandswing extends jframe{static thread thread;     JProgressBar ProgressBar;         Public threadandswing () {progressbar=new jprogressbar ();         Progressbar.setstringpainted (TRUE);         Container Container=getcontentpane (); Container.add (ProgressBar, Borderlayout.north);//If you do not specify a layout manager, the default is to use BorderLayout.         If you do not use the layout manager, you need to specify the setlayout (new BorderLayout ()) This.settitle ("thread interruption in the use of the Swing progress bar");         This.pack ();         This.setvisible (TRUE);                  This.setdefaultcloseoperation (Windowconstants.exit_on_close);         This.creatthread (); Thread.Start (); Thread_instance.setcontinue (FALSE); Another way to break the thread Thread.interruPT ();         } class Thread_instance implements runnable{Boolean iscontinue=true;         public void Setcontinue (Boolean b) {this.iscontinue=b;                          } @Override public void Run () {//TODO auto-generated method stub int count=0;                 while (true) {progressbar.setvalue (++count);                 try {thread.sleep (1000); } catch (Interruptedexception e) {//TODO auto-generated catch block System.out.                     println ("Current program is interrupted");                 Break                 } if (!iscontinue) {break;         }} System.out.println ("Here");     }} void Creatthread () {thread=new thread (new Thread_instance ());     } static void init (JFrame frame,int width,int height) {frame.setsize (width,height); } public static VoiD main (string[] args) {init (new threadandswing (), 300,100);  } }


The Java Swing thread performs the task (otherwise the interface is always in a non-clickable state)

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.