java中jprogressbar_java – JProgressBar不会更新

我正在尝试从这个问题的投票答案中找到的代码: Download file using java apache commons?

这是一个下载应用程序,看一看,(我对JFrames和ActionEvents不是很熟悉)

Download.java

package main;

public class Download extends JFrame implements Runnable{

public static int total;

public static int done;

private static class ProgressListener implements ActionListener{

@Override

public void actionPerformed(ActionEvent e) {

done = (int)((DownloadCountingOutputStream) e.getSource()).getByteCount();

jbar.repaint();

DownloadCountingOutputStream.parent.draw((int)((DownloadCountingOutputStream) e.getSource()).getByteCount());//redraw

DownloadCountingOutputStream.parent.repaint();

}

}

public static JProgressBar jbar = new JProgressBar();

public void draw(int downloaded){System.out.println("downloaded: "+downloaded+ " Total: "+total);

if (downloaded== 0){

Container cont = new Container();

setDefaultCloseOperation(3);

setSize(600, 450);

setResizable(false);

setVisible(true);

cont.add(jbar);

jbar.setBounds(40, 50, 500, 50);

jbar.setMaximum(total);//The total value of bytes to download

//jbar.setValue(50);

add(cont);

jbar.setVisible(true);

}

jbar.setValue(downloaded);

//This should update the value of the progress Bar

}

public void run() {

URL dl = null;

File fl = null;

OutputStream os = null;

InputStream is = null;

ProgressListener progressListener = new ProgressListener();

draw(done);

try {

fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/afile.rar");

dl = new URL("https://dl.dropbox.com/u/48076798/afile.rar");

os = new FileOutputStream(fl);

is = dl.openStream();

total = Integer.parseInt(dl.openConnection().getHeaderField("Content-Length"));

String total = dl.openConnection().getHeaderField("Content-Length");

DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);

dcount.setListener(progressListener);

dcount.setParent(this);

IOUtils.copy(is, dcount);

} catch (Exception e) {

System.out.println(e);

} finally {

if (os != null) {

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (is != null) {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

DownloadCountingOutputStream.java

package main;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.io.OutputStream;

import org.apache.commons.io.output.CountingOutputStream;

public class DownloadCountingOutputStream extends CountingOutputStream {

private ActionListener listener = null;

public static Download parent;

public DownloadCountingOutputStream(OutputStream out) {

super(out);

}

public void setListener(ActionListener listener) {

this.listener = listener;

}

public void setParent(Download o){

parent = o;

}

@Override

protected void afterWrite(int n) throws IOException {

super.afterWrite(n);

if (listener != null) {

listener.actionPerformed(new ActionEvent(this, 0, null));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值