Using Java to quickly implement a progress bar __java

Source: Internet
Author: User

Based on someone asking how to make a progress bar, here's a simple procedure:

Mainly using JProgressBar (Swing built-in Javax.swing.JProgressBar) and SwingWorker (Swing built-in Javax.swing.SwingWorker)

Some people will say, not by thread. It's OK to share a variable with a thread. But this is not a very elegant way, of course, you can use the thread to encapsulate the line. However, SwingWorker has helped us to encapsulate the ~ ~ ~ Use ready-made things to do or convenient ^_^

First look at the effect:

Look at the code below: very simple, I believe you are greatly sure to understand the ~

Package Org.ubird; Import Java.awt.BorderLayout; Import java.awt.Dimension; Import Java.awt.Image; Import java.awt.event.ActionEvent; Import Java.awt.event.ActionListener; Import java.util.ArrayList; Import java.util.List; Import java.util.concurrent.ExecutionException; Import Javax.imageio.ImageIO; Import Javax.swing.ImageIcon; Import Javax.swing.JButton; Import Javax.swing.JLabel; Import Javax.swing.JPanel; Import Javax.swing.JProgressBar; Import Javax.swing.JScrollPane; Import javax.swing.SwingUtilities; Import Javax.swing.SwingWorker; public class Testswingworker extends swingworker<list<image>,string>{private JLabel status; Private JPanel Viewer Private string[] Imagesname; Private JProgressBar JPB; public static void Main (string[] args) {Swingutilities.invokelater (new Runnable () {public void run () {Testframe frame = New Testframe (); Final JPanel panel = new JPanel (); Final JLabel label = new JLabel (); Final string[] Imagesname = new string[38]; for (int i=0; i<iMagesname.length; i++) {Imagesname[i] = "RES/1 (" + (i+1) + "). jpg"; JScrollPane sp = new JScrollPane (panel); Sp.setsize (New Dimension (700,500)); Frame.add (Sp,borderlayout.center); JPanel stp = new JPanel (); Final JProgressBar JPB = new JProgressBar (); Jpb.setminimum (1); Jpb.setmaximum (imagesname.length); Stp.add (JPB); Stp.add (label); Frame.add (Stp,borderlayout.south); JButton button = new JButton ("Load image"); Button.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {testswingworker SW = new Test SwingWorker (label, panel, Imagesname, JPB); Sw.execute (); } }); Frame.add (Button,borderlayout.north); Frame.setvisible (TRUE); } }); Public Testswingworker (JLabel status, JPanel Viewer, string[] imagesname, JProgressBar jpb) {this.status = status; Viewer = Viewer; This.imagesname = Imagesname; THIS.JPB = JPB; } @Override protected list<image> Doinbackground () throws Exception {list<image> Image = new Arraylist<ima Ge> (); for (int i=0; I&LT;imagesname.length; i++) {Image.add (Imageio.read (GetClass (). getClassLoader (). GetResource (Imagesname[i))); Publish ("Already loaded" + (i+1) + "/" +imagesname.length+ ":" +imagesname[i]);} return image; @Override protected void process (list<string> chunks) {Status.settext (Chunks.get ()-1)); int x = Chunks.size Ger.parseint (Chunks.get (Chunks.size ()-1). substring (Chunks.get (chunks.size ()-1). IndexOf ("()) +1,chunks.get ( Chunks.size ()-1). IndexOf (")"). Trim ()); Jpb.setvalue (x); for (String str:chunks) {System.out.println (str);}} @Override protected void Do () {try {for (Image Image:get ()) {JLabel label = new JLabel (new ImageIcon (image); label.s Etsize (160, 120); Label.setpreferredsize (New Dimension (160,120)); Viewer.add (label); The catch (Interruptedexception e) {e.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ();}} }

Package Org.ubird; Import Javax.swing.JFrame; public class Testframe extends JFrame {public testframe () {settitle ("Test Frame"); SetSize (800,600); setdefaultcloseoper ation (Jframe.exit_on_close); } }

The program can run the source code, resource picture Download address: http://download.csdn.net/source/3245370

Note: after decompression, double-click Inside: Compileandrun.bat can run directly under win

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.