使用Java高速實現進度條

來源:互聯網
上載者:User

標籤:http   java   使用   os   io   strong   for   ar   

基於有人問到如何做進度條,以下給個簡單的做法:

主要是使用JProgressBar(Swing內建javax.swing.JProgressBar)和SwingWorker(Swing內建javax.swing.SwingWorker)

 

有人肯定會說,不是用線程做的嗎?使用線程共用一個變數,確實能夠。只是這樣的方式不太優雅,當然,你能夠再用線程封裝也行。只是,SwingWorker已經幫我們封裝好啦~~使用現成的東西來做還是方便^_^

 

先看效果:

 

 

 

 

 

 

 

 

以下看代碼:非常easy,相信各位大大肯定是懂的~

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 TestSwingWorker(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;this.viewer = viewer;this.imagesName = imagesName;this.jpb = jpb;}@Overrideprotected List<Image> doInBackground() throws Exception {List<Image> image = new ArrayList<Image>();for(int i=0; i<imagesName.length; i++){image.add(ImageIO.read(getClass().getClassLoader().getResource(imagesName[i])));publish("已經載入了 "+(i+1)+"/"+imagesName.length+" : "+imagesName[i]);}return image;}@Overrideprotected void process(List<String> chunks) {status.setText(chunks.get(chunks.size()-1));int x = Integer.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);}}@Overrideprotected void done() {try {for(Image image : get()){JLabel label = new JLabel(new ImageIcon(image));label.setSize(160, 120);label.setPreferredSize(new Dimension(160,120));viewer.add(label);}} 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);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

 

 

該程式的可執行原始碼、資源圖片的:http://download.csdn.net/source/3245370

NOTE:解壓後雙擊裡面的:CompileAndRun.bat可在Win下直接執行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.