Small thread example -- progress bar, thread example -- progress bar
Example of a small thread -- progress bar
Function: displays a progress bar in the form. The progress bar increases automatically.
Import java. awt. container; import java. awt. flowLayout; import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JProgressBar; public class ThreadDemo extends JFrame {Thread thread; public ThreadDemo () {super (); Container c = getContentPane (); setBounds (300,200,250,100 ); final JProgressBar B = new JProgressBar (); // progress bar c. add (B); B. setStringPainted (true); // set the progress bar to display the number thread = new Thread (new Runnable () {int count = 0; @ Override public void run () {// TODO Auto-generated method stub while (true) {B. setValue (count ++); // set the current value of the progress bar. try {Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}}); thread. start (); setVisible (true);} public static void main (String [] args) {new ThreadDemo ();}}
The only thing that is needed is probably the knowledge point of the progress bar:
JProgressBar:
SetStringPainted: Set whether a number is displayed on the progress bar.
SetValue sets the current value of the progress bar
The result is as follows: