finalJButtonbutton=newJButton("下载");button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){newThread(){@Overridepublicvoidrun(){button.setEnabled(false);//设置按钮在下载过程中不可操作FileOutputStreamfos=null;try{URLurl=newURL(txtFilefjavaindexhtml.getText());//建立HTTP连接URLConnectionconn=url.openConnection();//创建连接对象intfileSize=conn.getContentLength();//用getContentLength()得到连接对象的文件大小InputStreamis=conn.getInputStream();byte[]buffer=newbyte[1024];intlength=0;fos=newFileOutputStream(txtFilefjavaindexhtml_1.getText());doubledownSize=0;while(-1!=(length=is.read(buffer))){downSize+=length;doublepercent=downSize*100/fileSize;//计算下载百分比fos.write(buffer,0,length);progressBar.setValue((int)percent);//设置进度条变化}}catch(MalformedURLExceptione1){e1.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}finally//输出流要记得在fanally关掉{button.setEnabled(true);//在finally中恢复按钮可操作if(null!=fos){try{fos.close();}catch(IOExceptione1){e1.printStackTrace();}}}}}.start();}});button.setFont(newFont("宋体",Font.PLAIN,24));button.setBounds(10,164,97,37);frame.getContentPane().add(button);progressBar=newJProgressBar();progressBar.setBounds(152,164,272,37);frame.getContentPane().add(progressBar);//进度条按钮