public interface MyCommandListener extends ActionListener{
//设置资源路径,文件保存路径,线程数量
public void setUrlInfo(JTextField urlText);
public void setFilePath(JTextField fileText);
public void setThreadNum(JTextField threadNum);
}
5、OnClickListener
public class OnClickListener implements MyCommandListener{
String urlPath=null;
String filePath=null;
int threadNum;
JTextField urlText;
JTextField filePathText;
JTextField threadNumInfo;
public void setUrlInfo(JTextField urlText)
{
this.urlText=urlText;
}
public void setFilePath(JTextField fileText)
{
this.filePathText=fileText;
}
public void setThreadNum(JTextField threadNum)
{
this.threadNumInfo=threadNum;
}
@Override
public void actionPerformed(ActionEvent e) {
urlPath=urlText.getText();
filePath=filePathText.getText();
//获取所需线程数
threadNum=Integer.parseInt(threadNumInfo.getText());
//开启多线程下载
MultiThreadManager mtDown=new MultiThreadManager(urlPath,filePath,threadNum);
try {
mtDown.downLoad();
} catch (Exception ex) {
ex.printStackTrace();
}
//创建进度对话框
final ProgressMonitor dialog=new ProgressMonitor(null,"等待任务完成","已完成:",0,100);
//在新线程中处理耗时任务
new Thread(()->
{
//绘制进度条
while(mtDown.getCompleteRate()<101)
{
dialog.setProgress(mtDown.getCompleteRate());
if(dialog.isCanceled())
{
break;
}
try{
Thread.sleep(300);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}).start();
}
}
Appreciating the commitment you put into your website and detailed information you provide. It’s good to come across a blog every once in a while that isn’t the same unwanted rehashed material. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.
Appreciating the commitment you put into your website and detailed information you provide. It’s good to come across a blog every once in a while that isn’t the same unwanted rehashed material. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.
Thank you for reading my article.I would appreciate it if you could share my website with your friends!