Java swing progress bar-simulate uninstallation, with effect diagram

Source: Internet
Author: User

Use timer, progressbar, and two buttons to create the applet. Click start to Unmount and stop.

The Code is as follows:

----------------------------------------------- The following is the code area --------------------------------------------------------------------------

Import Java. AWT. borderlayout; import Java. AWT. dimension; 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. jprogressbar; import javax. swing. timer; import javax. swing. event. changeevent; import javax. swing. event. changelistener; public class progressbar extends jframe implements changelistener, actionlistener {jbutton start, stop; jprogressbar bar; jlabel show; jpanel pane, pane1; timer; Public progressbar (String title) {super (title); Init (); Pack ();} public void Init () {setbounds (300,100,300,100); pane = new jpanel (); pane. setlayout (New borderlayout (); getcontentpane (). add (PANE); Start = new jbutton ("START"); start. addactionlistener (this); stop = new jbutton ("stop"); stop. addactionlistener (this); pane1 = new jpanel (); pane1.add (start); pane1.add (STOP); bar = new jprogressbar (); Bar. setminimum (0); Bar. setmaximum (100); Bar. setvalue (100); Bar. setstringpainted (true); Bar. addchangelistener (this); Bar. setpreferredsize (new dimension (200, 30); int leave = 100-bar. getvalue (); show = new jlabel ("completed:" + leave, jlabel. center); pane. add (pane1, borderlayout. north); pane. add (Bar, borderlayout. center); pane. add (show, borderlayout. south); timer = new timer (50, this) ;}@ overridepublic void statechanged (changeevent e) {// todo auto-generated method stubint value = bar. getvalue (); int leave = 100-value; If (E. getsource () = bar) {Show. settext ("completed:" + leave + "%") ;}@overridepublic void actionreceivmed (actionevent e) {// todo auto-generated method stubif (E. getsource () = Start) {timer. start ();} else if (E. getsource () = stop) {timer. stop (); Bar. setvalue (0);} else if (E. getsource () = timer) {int value = bar. getvalue (); If (value> 0) {value --; Bar. setvalue (value) ;}} public static void main (string [] ARGs) {New progressbar ("Uninstall "). setvisible (true );}}

------------------------------------------------------ The above is the code area ------------------------------------------------------------------------------

The running effect is as follows:

--------- ------------

Note: This code can be directly copied to run

Related Article

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: 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.