package com.java.download1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;import com.java.download1.DownUtil;
import sun.security.pkcs11.P11TlsKeyMaterialGenerator;public class MultiThreadDown {
JFrame f=new JFrame("下载");
JLabel l1=new JLabel("下载路径:");
final JLabel l2=new JLabel("保存名称:");
final JLabel l3=new JLabel("下载进度:");
final TextField t1=new TextField("http://61.164.87.150:5483/pluginfile.php/1/theme_moodle360_1/logo/0/zzg_960_156.jpg");
final TextField t2=new TextField("nz.jpg");
final JProgressBar pb1 = new JProgressBar();
JButton b1=new JButton("下载");
public void init(){
f.setVisible(true);
f.setLayout(null);
f.setSize(500,300);
f.setLocation(200,200);
l1.setLocation(50,50);
l1.setSize(70,50);
l2.setLocation(50,100);
l2.setSize(70,50);
l3.setLocation(50,150);
l3.setSize(70,50);
t1.setLocation(120,65);
t1.setSize(300,30);
t2.setLocation(120,112);
t2.setSize(300,30);
b1.setLocation(200,230);
b1.setSize(70,30);
pb1 .setLocation(120,160);
pb1 .setSize(300,30);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(b1);
f.add(pb1);
pb1.setStringPainted(true);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
final DownUtil downUtil = new DownUtil(t1.getText(),t2.getText() , 3); try {
downUtil.download();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} new Thread(new Runnable() {
private int i;
public void run() {
while(downUtil.getCompleteRate()<1){
while(i<10){
pb1.setValue(Integer.valueOf((int)(downUtil.getCompleteRate()*100)));//这句话不对
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
});
}
public static void main(String[] args) throws Exception {
MultiThreadDown m1=new MultiThreadDown();
m1.init();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;import com.java.download1.DownUtil;
import sun.security.pkcs11.P11TlsKeyMaterialGenerator;public class MultiThreadDown {
JFrame f=new JFrame("下载");
JLabel l1=new JLabel("下载路径:");
final JLabel l2=new JLabel("保存名称:");
final JLabel l3=new JLabel("下载进度:");
final TextField t1=new TextField("http://61.164.87.150:5483/pluginfile.php/1/theme_moodle360_1/logo/0/zzg_960_156.jpg");
final TextField t2=new TextField("nz.jpg");
final JProgressBar pb1 = new JProgressBar();
JButton b1=new JButton("下载");
public void init(){
f.setVisible(true);
f.setLayout(null);
f.setSize(500,300);
f.setLocation(200,200);
l1.setLocation(50,50);
l1.setSize(70,50);
l2.setLocation(50,100);
l2.setSize(70,50);
l3.setLocation(50,150);
l3.setSize(70,50);
t1.setLocation(120,65);
t1.setSize(300,30);
t2.setLocation(120,112);
t2.setSize(300,30);
b1.setLocation(200,230);
b1.setSize(70,30);
pb1 .setLocation(120,160);
pb1 .setSize(300,30);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(b1);
f.add(pb1);
pb1.setStringPainted(true);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
final DownUtil downUtil = new DownUtil(t1.getText(),t2.getText() , 3); try {
downUtil.download();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} new Thread(new Runnable() {
private int i;
public void run() {
while(downUtil.getCompleteRate()<1){
while(i<10){
pb1.setValue(Integer.valueOf((int)(downUtil.getCompleteRate()*100)));//这句话不对
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
});
}
public static void main(String[] args) throws Exception {
MultiThreadDown m1=new MultiThreadDown();
m1.init();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货