Java Swing Program Development-lower right corner show progress bar

Source: Internet
Author: User

Although I do not like Java GUI program, but the cause of work, obey the superior arrangement.

To the point: This article is implemented in the lower right corner of the desktop display a progress bar, showing the current program running progress. And the mouse can drag the progress bar. Children's shoes are needed, can be copied directly to the past use of the code!

Java Reference Code:

Import Java.awt.dimension;import java.awt.insets;import Java.awt.toolkit;import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;import Java.awt.event.mousemotionlistener;import Javax.swing.JDialog;import Javax.swing.jprogressbar;import Javax.swing.swingconstants;public Class ProgressDialog extends JDialog implements Mouselistener,mousemotionlistener {private static final long Serialversionuid = 1l;private JProgressBar progressBar; private int height = 20;//progress bar height private int width = 260;//progress bar width private int mousex;//mouse x coordinate private int mousey;//mouse y coordinate private int currentx;//progress bar current x coordinate private int currenty;//progress bar current y-coordinate private Boolean ismove = False;public progressdialog (int number) { Initialize (); Initprogressbar (number); This.add (ProgressBar);} /** * Initialize form */private void Initialize () {Dimension Dimension = Toolkit.getdefaulttoolkit (). Getscreensize ();//Screen size insets Screeninsets = Toolkit.getdefaulttoolkit (). Getscreeninsets (This.getgraphicsconfiguration ()); CurrentX = (int) ( Dimension.getwidth () -width-3); currenty = (int) (Dimension.getheight ()-screeninsets.bottom-height-3); This.setalwaysontop (true); THIS.S Etsize (width, height); this.setlocation (CurrentX, currenty); this.setundecorated (true);} /** * @param number progress bar Max * Initialize progress bar */private void Initprogressbar (int number) {ProgressBar = new JProgressBar (Swingconstan Ts. Horizontal);p rogressbar.setminimum (0);p rogressbar.setmaximum (number);p rogressbar.setstringpainted (TRUE);// Displays the progress bar value Progressbar.addmousemotionlistener (this);p the Rogressbar.addmouselistener (this);} Public JProgressBar Getprogressbar () {return progressBar;} @Overridepublic void mousedragged (MouseEvent e) {if (ismove) {//mouse drag handling int dx = E.getxonscreen ()-Mousex;int dy = E.getyon Screen ()-mousey;this.setlocation (currentx + dx, currenty + dy);}} @Overridepublic void mousemoved (MouseEvent e) {//TODO auto-generated method stub} @Overridepublic void Mouseclicked ( MouseEvent e) {//TODO auto-generated method stub} @Overridepublic void Mousepressed (MouseEvent e) {ismove = true;//Remember to drag the starting coordinates mousex = E.getxonscreen (); mousey = E.getyonscreen ();} @Overridepublic void mousereleased (MouseEvent e) {//TODO auto-generated method stub} @Overridepublic void Mouseentered ( MouseEvent e) {//Todo auto-generated method stub} @Overridepublic void Mouseexited (MouseEvent e) {//Todo auto-generated m Ethod stub}public static void Main (string[] args) {int max = 20; ProgressDialog Dlgmain = new ProgressDialog (max);d lgmain.setvisible (true); JProgressBar ProgressBar = Dlgmain.getprogressbar (); for (int i = 0; i < max; i++) {Progressbar.setvalue (i + 1); try {Thr Ead.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}} Dlgmain.dispose ();}}

(Take a look at the bottom right corner):


Java Swing Program Development-lower right corner show progress bar

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.