/** * 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)