使用Java高速实现进度条

基于有人问到如何做进度条,以下给个简单的做法:

主要是使用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; } @Override protected 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; } @Override protected 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); } } @Override protected 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下直接执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值