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

📄 downloadmanager.java

📁 1.代码全由java书写 2.支持http1.1的可续传下载 3.GUI采用的是Java Swing 4.设计模式为Observer 5.采用了多线程机制
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//创建于2007-12-22 by刘浩
//最后更改于2008-12-28 by兰冲
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.*;

//下载管理器
public class DownloadManager extends JFrame
        implements Observer {

	//提示输入url
	 private JLabel addLabel;
	
	//输入url的文本框
    private JTextField addTextField;   
   
    //提示选择保存路径
    private JLabel saveDirLabel;
    
    //暂存选择的存储目录
    private JTextField saveDirTextField;
    
    //提示设置文件另存名称
    private JLabel saveNameLabel;
    
    //输入另存名称的文本框
    private JTextField saveNameTextField;
    
    //选择目录的按钮
    private JButton chooseDirButton;
    
    //添加下载的按钮
    JButton addButton;
    
    // 下载table的数据模型
    private DownloadsTableModel tableModel;
    
    // 存储下载信息的表
    private JTable table;

    // 这些是管理选中下载任务的按钮.
    private JButton pauseButton,  resumeButton;
    private JButton cancelButton,  clearButton;

    // 当前选中的下载任务
    private Download selectedDownload;

    // 选中下载任务是否已被清除的标志
    private boolean clearing;

    // Constructor
    public DownloadManager() {
        // 设置窗体标题
    	Color backColor = new Color(133,173,188);
    	this.getContentPane().setBackground(backColor);
    //	this.getContentPane().getComponents().
    	
    	
        setTitle("下载管理器");

        // 设置窗口大小
        
        setSize(640, 580);
        setLocation(250,100);
        getContentPane().setLayout(null);
        this.setResizable(false);
        // 处理窗口关闭事件
        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                actionExit();
            }
        });

        // 建立文件菜单
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("文件(F)");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem fileExitMenuItem = new JMenuItem("退出(X)",
                KeyEvent.VK_X);
        fileExitMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                actionExit();
            }
        });
        fileMenu.add(fileExitMenuItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
       
        //本窗口的页面采用无布局,下面的变量用作定位和设置控件大小
        int x1 = 20, x2 = 100, x3 = 505, x4 = 250;
        int y1 = 30, y2 = y1 + 30, y3 = y2+30, y4 = y3+30;
        int w1 = 70, w2 = 400, w3 = 80, w4 = 80;
        int h = 20;
        
        int panelx = x1;
        int panely1 = 20, panely2 = y4+h+30, panely3 = 460;
        int panelw = 600;
        int panelh1 = panely2 - panely1;
        int panelh2 = panely3 - panely2;
        int panelh3 = 40;
        
        // 构造添加下载任务的面板addPanel
        JPanel addPanel = new JPanel();
        
        addPanel.setLocation(panelx, panely1);
        addPanel.setSize(panelw, panelh1);
        addPanel.setBorder(
                BorderFactory.createTitledBorder("新建下载任务"));
         addPanel.setLayout(null);
         addPanel.setBackground(backColor);
     
       
       
        
        //待下载文件地址
        addLabel = new JLabel("网址(URL):");
        addLabel.setLocation(x1, y1);
        addLabel.setSize(w1, h);
        
        addPanel.add(addLabel);        
        addTextField = new JTextField(30);
        addTextField.setLocation( x2, y1);
        addTextField.setSize(w2, h);
       
        addTextField.getDocument().addDocumentListener(new javax.swing.event.DocumentListener(){
            /**
             *设置url输入框改变的事件监听器,实现当url改变时更新另存名称文本框中的内容
             *
             * @param e the document event
             */
        	public void changedUpdate(DocumentEvent e) {
                
        		String url = addTextField.getText();
        		URL verifiedUrl = verifyUrl(url);
        		if ( verifiedUrl != null )
        		{
        			saveNameTextField.setText(getFileName(verifiedUrl));
        		}
            }
            /**
             * Gives notification that there was an insert into the document. The 
             * range given by the DocumentEvent bounds the freshly inserted region.
             *
             * @param e the document event
             */    
            public void insertUpdate(DocumentEvent e) {
                // TODO 自动生成方法存根
            	String url = addTextField.getText();
        		URL verifiedUrl = verifyUrl(url);
        		if ( verifiedUrl != null )
        		{
        			saveNameTextField.setText(getFileName(verifiedUrl));
        		}
            }
            /**
             * Gives notification that a portion of the document has been 
             * removed. The range is given in terms of what the view last
             * saw (that is, before updating sticky positions).
             *
             * @param e the document event
             */
            public void removeUpdate(DocumentEvent e) {
             
            	String url = addTextField.getText();
        		URL verifiedUrl = verifyUrl(url);
        		if ( verifiedUrl != null )
        		{
        			saveNameTextField.setText(getFileName(verifiedUrl));
        		}
            }

        }
            );
        addPanel.add(addTextField);  
      
      
        
        //存储目录
        saveDirLabel = new JLabel("存储目录:");
        saveDirLabel.setLocation(x1, y2);
        saveDirLabel.setSize(w1,h);
        addPanel.add(saveDirLabel); 
        
        saveDirTextField = new JTextField("");
        saveDirTextField.setLocation(x2,y2);
        saveDirTextField.setSize(w2, h);
        addPanel.add(saveDirTextField); 
        
        chooseDirButton = new JButton("浏览");
        chooseDirButton.setLocation(x3, y2);
        chooseDirButton.setSize(w3,h);
        
        Font myFont = new Font("宋体",Font.BOLD, 12);
        chooseDirButton.setFont(myFont);      
        
       // chooseDirButton.setfon
        chooseDirButton.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent arg0) {
        		actionChooseSaveDir();        		
        	}
        });
        addPanel.add(chooseDirButton);
        
        
        
        //另存名称
        saveNameLabel = new JLabel("另存名称:");
        saveNameLabel.setLocation(x1, y3);
        saveNameLabel.setSize(w1,h);
        addPanel.add(saveNameLabel);
        
        saveNameTextField = new JTextField(30);
        saveNameTextField.setLocation(x2,y3);
        saveNameTextField.setSize(w2,h);
        addPanel.add(saveNameTextField);
        
       
        
        
        
        //下载按钮    
        addButton = new JButton("下载");
        addButton.setLocation(x4, y4);
        addButton.setForeground(Color.BLACK);
        addButton.setSize(w4, h);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                actionAdd();
            }
        });
        addPanel.add(addButton);
       
        
        
        
        //设置窗体的第二个部分下载任务列表    
        tableModel = new DownloadsTableModel();
        table = new JTable(tableModel);
      //  table.setBackground(backColor);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                tableSelectionChanged();
            }
        });
        // 一次只能选择一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        // 将ProgessRenderer作为table的Progress列的renderer
        ProgressRenderer renderer = new ProgressRenderer(0, 100);
        renderer.setStringPainted(true); // show progress text
        table.setDefaultRenderer(JProgressBar.class, renderer);
        

⌨️ 快捷键说明

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