package data;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;public class Download6 extends FocusAdapter implements Runnable,ActionListener{ JFrame frame; JTextField url_txt,dir_txt,name_txt; JButton open_btn,start_btn; JProgressBar pro; JLabel l5; JTextArea mes_txt; static JFileChooser chooser; String dir; static Thread thread; String urls,path; int ID; long start,end; //初始化参数 public Download6(String urls,String path,int ID,long start,long end,JTextArea mes_txt) { this.urls=urls; this.path=path; this.ID=ID; this.start=start; this.end=end; this.mes_txt=mes_txt; } //图形界面初始化 public Download6(String title) { frame=new JFrame(title); frame.setLayout(null); JLabel l1=new JLabel("网址(URL):"); frame.add(l1); l1.setBounds(10,10,70,25); url_txt=new JTextField(20); url_txt.setText("http://"); url_txt.setCaretPosition(url_txt.getText().length()); url_txt.setFont(new Font(null,Font.BOLD,15)); url_txt.addFocusListener(this); frame.add(url_txt); url_txt.setBounds(75,10,315,25); JSeparator line=new JSeparator(); frame.add(line); line.setBounds(10,45,380,25); JLabel l2=new JLabel("存放目录:"); frame.add(l2); l2.setBounds(10,60,70,25); dir_txt=new JTextField(15); dir_txt.setText("请选择存放目录"); dir_txt.setEditable(false); frame.add(dir_txt); dir_txt.setBounds(75,60,250,25); open_btn=new JButton("浏览"); frame.add(open_btn); open_btn.setBounds(325,60,65,25); JLabel l3=new JLabel("另存名称:"); frame.add(l3); l3.setBounds(10,95,70,25); name_txt=new JTextField(20); frame.add(name_txt); name_txt.setBounds(75,95,315,25); name_txt.setFont(new Font(null,Font.BOLD,15)); mes_txt=new JTextArea(); mes_txt.setEditable(false); mes_txt.setWrapStyleWord(true); JScrollPane sp=new JScrollPane(mes_txt,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.add(sp); sp.setBounds(10,125,380,80); pro=new JProgressBar(); pro.setStringPainted(true); frame.add(pro); pro.setBounds(10,210,380,15); JLabel l4=new JLabel("@逛悠人作品^_^"); frame.add(l4); l4.setBounds(10,240,100,25); l5=new JLabel(); frame.add(l5); l5.setBounds(150,240,100,25); start_btn=new JButton("开始下载"); frame.add(start_btn); start_btn.setBounds(290,240,100,25); frame.setLocation(300,200); frame.setSize(405,300); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); open_btn.addActionListener(this); start_btn.addActionListener(this); } //监听两个按钮方法 public void actionPerformed(ActionEvent e) { Object s=e.getSource(); if(s.equals(open_btn)) { openfile(); } else { String urls=url_txt.getText(); try { long time=System.currentTimeMillis()/1000; URL url=new URL(urls); URLConnection con=url.openConnection(); int filelength=con.getContentLength(); int num=10; int size=filelength/num; String path=dir_txt.getText()+name_txt.getText(); Thread t=null; CountTime count=new CountTime(urls,path,name_txt.getText(),time,l5,pro,mes_txt,start_btn); count.start(); for(int i=0;i
java多线程下载源码_java多线程下载源代码分享
最新推荐文章于 2023-06-26 15:29:14 发布