mainframe.java

来自「一款JAVA开发的类迅雷的多线程下载系统,希望对大家有帮助」· Java 代码 · 共 1,626 行 · 第 1/5 页

JAVA
1,626
字号
    	    tablepopup.add(redownmenu);
    	    redownmenu.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		int result = JOptionPane.showConfirmDialog(null,"重新下载会删除上次已经下载的文件,是否继续......",
    	    				"提示",JOptionPane.YES_NO_OPTION);
    	    		if(result == JOptionPane.YES_OPTION){
    	    			tablemodel.addDownload(selectedDownload);    
    	    			selectionNode.downloadList.remove(selectedDownload);
    	    			table.setModel(tablemodel);
    	    			tree.setSelectionRow(1);
    	    		}
    	    	}
    	    });
    	    tablepopup.add(detmenu);
    	    detmenu.setIcon(detpic);
    	    detmenu.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		actionClear();
    	    		tablemodel.fireTableDataChanged();
    	    	}
    	    });
    	    PopupListener tablepo = new PopupListener(tablepopup);
    	    table.addMouseListener(tablepo);
        }
        
        //构键TREE右键弹出菜单
        private void buildTreepopup(){
        	treepopup.add(newleibie);
        	newleibie.setIcon(newpic);
    	    newleibie.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		xinjianleibie = new XinJianLeiBieDialog(main,"新建类别",treeModel,selectionNode);
    	    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	            Dimension mainSize = xinjianleibie.getSize();
    	            xinjianleibie.setLocation((screenSize.width - mainSize.width) / 2,
    	                 (screenSize.height - mainSize.height) / 2);
    	    	    xinjianleibie.setResizable(false);
    	    	    xinjianleibie.setVisible(true);
    	    	}
    	    });
    	    treepopup.add(openleimenu);
    	    openleimenu.setIcon(openmenupic);
    	    treepopup.addSeparator();
    	    openleimenu.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		if(selectionNode != null){
    	    			try {
    	    				java.lang.Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE "+selectionNode.lujing);
    	    			} catch (IOException ex) {
    	    				ex.printStackTrace();
    	    			}
    	    		}
    	    	}
    	    });
    	    treepopup.add(detleibie);
    	    detleibie.setIcon(detpic);
    	    detleibie.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		        updateTreeYoujian();
    	    				for(int i=0;i<leadNodeList.size();i++){
    	    					TreeModel treemodel = (TreeModel)leadNodeList.get(i);
        	    				if(selectionNode.getUserObject().equals(treemodel.name)){
        	    					File file = new File("doc/"+selectionNode.name);
            	    				file.deleteOnExit();
        	    					leadNodeList.remove(i);
        	    				}
        	    			}
    	    				treeModel.removeNodeFromParent(selectionNode);	    			
    	    		}
    	    });
    	    treepopup.add(qingkong);
    	    qingkong.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		node3.downloadList.clear();
    	    		DownloadsTableModel tablemodel = (DownloadsTableModel) table.getModel();
    	    		tablemodel.fireTableDataChanged();
    	    	}
    	    });
    	    treepopup.add(shuxing);
    	    treepopup.addSeparator();
    	    shuxing.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		shuxingdialog = new ShuXingDialog(main,"属性",selectionNode);
    	    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	            Dimension mainSize = shuxingdialog.getSize();
    	            shuxingdialog.setLocation((screenSize.width - mainSize.width) / 2,
    	                 (screenSize.height - mainSize.height) / 2);
    	            shuxingdialog.setResizable(false);
    	    	    shuxingdialog.setVisible(true);
    	    	}
    	    });
    	    PopupListener treepo = new PopupListener(treepopup);
    	    tree.addMouseListener(treepo);
        }
        
        //构建类别树结构
        private void buildTree(){
        	root.add(node1);
            root.add(node2);
            root.add(node3);
            dettablemodel = new DownloadsTableModel(node3);
            readTreeNode();
            if(!read){
					try {
						BufferedWriter f2=new BufferedWriter(new FileWriter("doc/lishi.txt"));
						f2.write("1电影");
						f2.newLine();
						f2.write("D:\\");
						f2.newLine();
						f2.write("2软件");
						f2.newLine();
						f2.write("D:\\");
						f2.newLine();
						f2.write("3游戏");
						f2.newLine();
						f2.write("D:\\");
						f2.newLine();
						f2.write("4图片");
						f2.newLine();
						f2.write("D:\\");
						f2.newLine();
						f2.write("5音乐");
						f2.newLine();
						f2.write("D:\\");
						f2.newLine();
						f2.flush();
						f2.close();
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}		
            	readTreeNode();
            }
    	    tree = new JTree(root);
    	    jsptree = new JScrollPane();
    	    jsptree.setHorizontalScrollBarPolicy(JScrollPane.
    	            HORIZONTAL_SCROLLBAR_NEVER);
    	    jsptree.setVerticalScrollBarPolicy(JScrollPane.
    	          VERTICAL_SCROLLBAR_AS_NEEDED);
    	    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.addTreeSelectionListener(this);
    	    tree.addMouseListener(new MouseAdapter(){
    	    	public void mouseClicked(MouseEvent e){
    	    		if(e.getClickCount() == 2){
                       if(selectionNode != root&&selectionNode != node1&&selectionNode != node3){
    	    			shuxingdialog = new ShuXingDialog(main,"属性",selectionNode);
        	    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        	            Dimension mainSize = shuxingdialog.getSize();
        	            shuxingdialog.setLocation((screenSize.width - mainSize.width) / 2,
        	                 (screenSize.height - mainSize.height) / 2);
        	            shuxingdialog.setResizable(false);
        	    	    shuxingdialog.setVisible(true);
    	    		}
    	    	  }
    	    	}
    	    });
    	    treeModel = (DefaultTreeModel)tree.getModel();
    	    tree.setSelectionRow(1);
    	    jsptree.getViewport().add(tree);
        }
        
        //构建TABLE
        private void buildTable(){
        	scrollpane = new JScrollPane();
    	    scrollpane.setHorizontalScrollBarPolicy(JScrollPane.
    	            HORIZONTAL_SCROLLBAR_NEVER);
    	    scrollpane.setVerticalScrollBarPolicy(JScrollPane.
    	          VERTICAL_SCROLLBAR_AS_NEEDED);
    	    tablemodel = new  DownloadsTableModel(Download.DOWNLOADING);    	    
    	    table = new JTable(tablemodel){
    	        public Component prepareRenderer(TableCellRenderer renderer,
                        int rowIndex, int vColIndex) {
    	        	Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
                 if (c instanceof JComponent){
                        JComponent jc = (JComponent)c;
                        DownloadsTableModel tabmodel =(DownloadsTableModel)table.getModel();
                        Download download = null;
                        if(tabmodel == tablemodel){
                        	if(rowIndex < tabmodel.downloadingList.size())
                           download = (Download)tabmodel.downloadingList.get(rowIndex);
                        }else{
                           download = (Download)tabmodel.downloadedList.get(rowIndex);
                        }
                        if(download != null)
                        jc.setToolTipText("<html>"+"文件名:"+download.getFileName()+"<br>"+
                        		"--------------------------------------------"+"<br>"+
                        		"文件大小:"+download.getSize()+"字节<br>"+
                        		"文件URL地址:"+download.getUrl()+"<br>"+
                        		"文件保存路径:"+download.getMenuname()+"<br>"+
                        		"--------------------------------------------"+"<br>"+
                        		"文件下载日期:"+download.getDate()+"<br>"+
                        		"文件完成日期:"+download.getDated()+"<br>"+"<br>"+
                        		"下载用时:"+download.getTimed()+"<br>"+
                        		"--------------------------------------------"+"<br>"+
                        		"<a href="+download.getMenuname()+">     打开文件</a>"+
                        		"<a href="+download.getMenuname()+">     打开目录</a>"+
                        		"<a href="+download.getMenuname()+">     打开文件</a>"+
                        		"<a href="+download.getMenuname()+">     打开文件</a>"+
                        		"                                            "+"<br>"+"</html>");   
                        }
                   return c;
              }    	            	        
            };           
    	    scrollpane.getViewport().add(table);
    	    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    	    	public void valueChanged(ListSelectionEvent e){
    	    		tableSelectionChanged();
    	    	}
    	    });
    	    table.addMouseListener(new MouseAdapter(){     //处理JTable双击事件
				public void mouseClicked(MouseEvent arg0){
				   if(table.getModel()==tablemodel){	
				   if(arg0.getClickCount() == 2){
				   if(selectedDownload != null){
					   if(selectedDownload.getStatus() == Download.DOWNLOADING)
						   actionPause();
					   else if(selectedDownload.getStatus() == Download.PAUSED)
						   actionResume();
					   else{};
				   }
				  }
				 }else{
					 if(arg0.getClickCount() == 2){
						 if(selectedDownload != null&&selectionNode!=node3){
		    	    			try {
		    	    				java.lang.Runtime.getRuntime().exec("cmd.exe /c"+selectedDownload.getMenuname()+"\\"+selectedDownload.getFileName());
		    	    				System.out.println(selectedDownload.getMenuname()+"\\"+selectedDownload.getFileName());
		    	    			} catch (IOException ex) {
		    	    				ex.printStackTrace();
		    	    			}
		    	    	  }
					 }
				 }
				}   	
    	    });
    	    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	    TableColumn column=null;
            
    	    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    	      for (int i=0;i<6;i++){
    	         //利用JTable中的getColumnModel()方法取得TableColumnModel对象;再利用TableColumnModel界面所定义的getColumn()方法取
    	         //TableColumn对象,利用此对象的setPreferredWidth()方法就可以控制字段的宽度.
    	      	  column=table.getColumnModel().getColumn(i);
    	      	  if (i==0)
    	      	     column.setPreferredWidth(20);
    	      	  else
    	      	  if(i == 1)
    	      	     column.setPreferredWidth(135);
    	      	  else 
    	      	  if(i==2)
    	      		  column.setPreferredWidth(60);
    	      	  else 
    	      	  if(i == 3)
    	      		  column.setPreferredWidth(60);
    	      	  else
    	      	  if(i == 5)
    	      	     column.setPreferredWidth(60);
    	      	  else 
    	      		 column.setPreferredWidth(130);
    	      }

    	    ProgressRenderer renderer = new ProgressRenderer(0,100);
    	    renderer.setStringPainted(true);
    	    table.setDefaultRenderer(JProgressBar.class,renderer);    	    
    	    table.setRowHeight(
    	    		(int) renderer.getPreferredSize().getHeight());    	   
    	    
    	    JLabelRenderer lbrenderer = new JLabelRenderer();
    	    table.setDefaultRenderer(JLabel.class,lbrenderer);
        }
        
        //构建菜单
        private void buildMenu(){
        	filemenu.setMnemonic('F');
        	filemenu.add(newmenuitem);
        	newmenuitem.setIcon(newpic);
    	    filemenu.addSeparator();
    	    newmenuitem.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		xinjian = new XinJianDialog(main,"新建任务",tablemodel);
    	    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	            Dimension mainSize = xinjian.getSize();
    	    	    xinjian.setLocation((screenSize.width - mainSize.width) / 2,
    	                 (screenSize.height - mainSize.height) / 2);
    	    	    xinjian.setResizable(false);
    	    	    xinjian.setVisible(true);
    	    	}
    	    });
    	    filemenu.add(startmenuitem);
    	    startmenuitem.setIcon(startpic);
    	    startmenuitem.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		actionResume();
    	    	}
    	    });
    	    filemenu.add(stopmenuitem);
    	    stopmenuitem.setIcon(stoppic);
    	    stopmenuitem.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		actionPause();
    	    	}
    	    });
    	    filemenu.add(detmenuitem);
    	    detmenuitem.setIcon(detpic);
    	    detmenuitem.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		actionClear();
    	    		tablemodel.fireTableDataChanged();
    	    	}
    	    });
    	    filemenu.addSeparator();
    	    filemenu.add(openmenuitem);
    	    openmenuitem.setIcon(openpic);
    	    openmenuitem.addActionListener(new ActionListener(){
    	    	public void actionPerformed(ActionEvent e){
    	    		if(selectedDownload != null){
    	    			try {
    	    				java.lang.Runtime.getRuntime().exec("cmd.exe /c"+selectedDownload.getMenuname()+"\\"+selectedDownload.getFileName());
    	    			} catch (IOException ex) {
    	    				ex.printStackTrace();
    	    			}
    	    		}
    	    	}
    	    });
    	    filemenu.add(openmenumenuitem);

⌨️ 快捷键说明

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