⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addtaskframe.java~8~

📁 多线程下载器。该小程序模范迅雷等下载器的下载原理实现多线程下载
💻 JAVA~8~
字号:
package ui;
import com.*;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.*;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class AddTaskFrame extends JFrame {
    private String filePath;
    private String fileName;
    private MainFrame frame;
    public AddTaskFrame() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public AddTaskFrame(MainFrame frame) {
        try {
            this.frame = frame;
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }


    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        this.setResizable(false);
        this.setSize(600,350);
        this.setLocation(300,200);
        this.setTitle("MyDownloader-添加任务");
        jLabel1.setText("地址:");
        jLabel1.setBounds(new Rectangle(35, 29, 42, 15));
        jLabel2.setText("保存路径:");
        jLabel2.setBounds(new Rectangle(35, 97, 64, 15));
        txtFilePath.setBounds(new Rectangle(63, 126, 360, 20));
        btnFile.setBounds(new Rectangle(442, 126, 81, 23));
        btnFile.setText("浏览");
        btnFile.addActionListener(new AddTaskFrame_btnFile_actionAdapter(this));
        btnStart.setBounds(new Rectangle(212, 204, 81, 23));
        btnStart.setText("开始下载");
        btnStart.addActionListener(new AddTaskFrame_btnStart_actionAdapter(this));
        jLabel3.setText("线程数:");
        jLabel3.setBounds(new Rectangle(37, 170, 57, 15));
        cboThreadNum.setBounds(new Rectangle(94, 167, 50, 23));
        btnCancel.setBounds(new Rectangle(313, 204, 81, 23));
        btnCancel.setText("取消");
        btnCancel.addActionListener(new AddTaskFrame_btnCancel_actionAdapter(this));
        this.getContentPane().add(jLabel1);
        this.getContentPane().add(txtURL);
        this.getContentPane().add(jLabel2);
        this.getContentPane().add(txtFilePath);
        this.getContentPane().add(btnFile);
        this.getContentPane().add(jLabel3);
        this.getContentPane().add(cboThreadNum);
        this.getContentPane().add(btnStart);
        this.getContentPane().add(btnCancel);
        txtURL.setBounds(new Rectangle(63, 52, 360, 20));
        for(int i = 1;i<=10;i++){
            this.cboThreadNum.addItem(i+"");
        }
    }

    JLabel jLabel1 = new JLabel();
    JTextField txtURL = new JTextField();
    JLabel jLabel2 = new JLabel();
    JTextField txtFilePath = new JTextField();
    JButton btnFile = new JButton();
    JButton btnStart = new JButton();
    JLabel jLabel3 = new JLabel();
    JComboBox cboThreadNum = new JComboBox();
    JButton btnCancel = new JButton();
    public void btnFile_actionPerformed(ActionEvent e) {
        JFileChooser jFileChooser = new JFileChooser("D:\\");
        int result;
        result = jFileChooser.showSaveDialog(this);
        jFileChooser.setFileSelectionMode(jFileChooser.DIRECTORIES_ONLY);
        if(result == JFileChooser.APPROVE_OPTION){
            this.filePath= jFileChooser.getSelectedFile().getAbsolutePath();
            this.txtFilePath.setText(filePath);
            fileName = jFileChooser.getSelectedFile().getName();
        }
        else{
            //System.out.println("bbb");
        }
    }

    public void btnStart_actionPerformed(ActionEvent e) {
        if(this.txtURL.getText().trim().equals("")){
            JOptionPane.showMessageDialog(null,"请输入网址");
        }
        else if(this.txtFilePath.getText().trim().equals("")){
            JOptionPane.showMessageDialog(null,"请输入文件保存路径");
        }
        else{
            DownloadInfo downInfo = new DownloadInfo();
            String url = this.txtURL.getText().trim();
            String fileName = this.fileName;
            int threadNum = this.cboThreadNum.getSelectedIndex()+1;
            String filePath = this.txtFilePath.getText().trim();
            downInfo.setUrl(url);
            downInfo.setFilename(fileName);
            downInfo.setThreadNum(threadNum);
            downInfo.setIsFinished(false);
            downInfo.setFilePath(filePath);
            this.frame.getDownInfos().add(downInfo);
            System.out.println(this.frame.getDownInfos());
            this.dispose();
        }

    }

    public void btnCancel_actionPerformed(ActionEvent e) {
        this.dispose();
    }


}


class AddTaskFrame_btnCancel_actionAdapter implements ActionListener {
    private AddTaskFrame adaptee;
    AddTaskFrame_btnCancel_actionAdapter(AddTaskFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnCancel_actionPerformed(e);
    }
}


class AddTaskFrame_btnStart_actionAdapter implements ActionListener {
    private AddTaskFrame adaptee;
    AddTaskFrame_btnStart_actionAdapter(AddTaskFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnStart_actionPerformed(e);
    }
}


class AddTaskFrame_btnFile_actionAdapter implements ActionListener {
    private AddTaskFrame adaptee;
    AddTaskFrame_btnFile_actionAdapter(AddTaskFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnFile_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -