mainframe.java

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

JAVA
1,626
字号
    					detleibie.setEnabled(true);
    					qingkong.setEnabled(false);
    					shuxing.setEnabled(true);
    				}
    			}
        }
        
		public void valueChanged(TreeSelectionEvent t) {
			JTree tree = (JTree) t.getSource();
			selectionNode = (TreeModel)tree.getLastSelectedPathComponent();	
			if(selectionNode!=null&&selectionNode.equals(node1)){
				table.setModel(tablemodel);
				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);
	    	      }
	    	   shuominglb.setText(selectionNode.downloadList.size()+"个文件");   
			}else{ 
				if(selectionNode != null){
				  tablemodel1 = new DownloadsTableModel(selectionNode);
				  table.setModel(tablemodel1);
				  TableColumn column=null;
	    	      for (int i=0;i<6;i++){
	    	         //利用JTable中的getColumnModel()方法取得TableColumnModel对象;再利用TableColumnModel界面所定义的getColumn()方法取
	    	         //TableColumn对象,利用此对象的setPreferredWidth()方法就可以控制字段的宽度.
	    	      	  column=table.getColumnModel().getColumn(i);
	    	      	  if (i==0)
	    	      	     column.setPreferredWidth(25);
	    	      	  else
	    	      	  if(i == 1)
	    	      	     column.setPreferredWidth(120);
	    	      	  else 
	    	      	  if(i==2)
	    	      		  column.setPreferredWidth(60);
	    	      	  else 
	    	      	  if(i == 3)
	    	      		  column.setPreferredWidth(160);
	    	      	  else
	    	      	     column.setPreferredWidth(140);
	    	       }
	    	      shuominglb.setText(selectionNode.downloadList.size()+"个文件");
			    }
			}
			selectedDownload = null;
			tabbedpane.removeAll();
			updateButtons();
			updateTreeYoujian();
		}
         
		//根据字符串查询是否存在相应的树结点
		public static TreeModel getTreeModel(String name){
			for(int i=0;i<leadNodeList.size();i++){
				TreeModel treeModel = (TreeModel)leadNodeList.get(i);
				if(name.equals(treeModel.name))
					return treeModel;
			}
			return null;
		}
			
		//根据字符串判断是否有相同的树结点已经存在
		public static boolean isSameName(String name){
			for(int i=0;i<leadNodeList.size();i++){
				TreeModel treeModel = (TreeModel)leadNodeList.get(i);
				if(name.equals(treeModel.name))
					return true;
			}
			return false;
		}
		
		 //获得图片资源
		 Image getImage(String filename) {
			    URLClassLoader urlLoader = (URLClassLoader)this.getClass().
			        getClassLoader();
			    URL url = null;
			    Image image = null;
			    url = urlLoader.findResource(filename);
			    image = Toolkit.getDefaultToolkit().getImage(url);
			    MediaTracker mediatracker = new MediaTracker(this);
			    try {
			      mediatracker.addImage(image, 0);
			      mediatracker.waitForID(0);
			    }
			    catch (InterruptedException _ex) {
			      image = null;
			    }
			    if (mediatracker.isErrorID(0)) {
			      image = null;
			    }
			    return image;
			  }
		 //为其他的类提供图片
		 public ImageIcon getImageIcon(int status){
			 switch(status){
				case Download.DOWNLOADING:
					return downing;
				case Download.PAUSED:
				    return pause;
				case Download.COMPLETE:
					return downed;
				case Download.CANCEL:
					return pause;
				case Download.ERROR:
					return detpic;
				case 5:
					return xinad;
				}
			 return pause;
		 }
		 
		 //读结点信息
		 public void readTreeNode(){
			 int i=0;
			 try {
				 BufferedReader f2=new BufferedReader(new FileReader("doc/lishi.txt"));				
					for(i=0;f2.read()!=-1;i++){						
						String name = f2.readLine();
						String lujing = f2.readLine();
						TreeModel leadNode = new TreeModel(name,false,lujing);
						node2.add(leadNode);
						leadNodeList.add(leadNode);
						if(read){
							read(leadNode,"doc/"+name+".txt");
					  }
					}
					if(read){
						read(node2,"doc/已经下载.txt");
						read(node3,"doc/垃圾箱.txt");					 
				  }		
				} catch (FileNotFoundException e) {
					System.out.println("i="+i);
					System.out.println("dakaiwenjianshipai");
					read = false;
				} catch (IOException e) {
					System.out.println("duwenjianshipai");
				}  
    
	      }
		 
		 //此方法仅共上面的readTreeNode()方法调用
		 public void read(TreeModel treemodel,String txtname){
			 try{
			 DataInputStream
			 dis1 = new DataInputStream(
					 new BufferedInputStream(new FileInputStream(txtname)));
			 if(treemodel == node2)
			treemodel.lujing = dis1.readUTF();
			int status1 = dis1.read();
			System.out.println("status1;"+status1);
			for(int j=0;status1!=-1;j++){
			String filename =dis1.readUTF();
			String filemenu =dis1.readUTF();
			long size =dis1.readLong();
			String urlstr = dis1.readUTF();
			String date = dis1.readUTF();
			String dated = null;
			if(treemodel != node3){
			 dated = dis1.readUTF();
			}
			String timed = dis1.readUTF();
//			URL url = new URL(urlstr);						
			Download download = new Download(urlstr,filename,filemenu,size,status1,date,dated,timed);
			treemodel.downloadList.add(download);
			status1 =dis1.read();
		   }
	       } catch (FileNotFoundException e) {
					System.out.println("dakaiwenjianshipai");
	       } catch (IOException e) {
					System.out.println("duwenjianshipai");
					e.printStackTrace();
		   }
		 }
		 
		 //此方法仅共writeTreeNode()方法调用
		 public void write(TreeModel treemodel,String txtname){
			 try{
			 DataOutputStream
			 dou1 = new DataOutputStream(
						new BufferedOutputStream(new FileOutputStream(txtname)));
			 if(treemodel == node2){
				dou1.writeUTF(node2.lujing);
				dou1.flush();
				System.out.println(node2.lujing);
			 }
				for(int j=0;j<treemodel.downloadList.size();j++){			    	
			    	   Download download = (Download) treemodel.downloadList.get(j);
			    	   int status = download.getStatus();
			    	   String filename = download.getFileName();
			    	   String filemenu = download.getMenuname();
			    	   long size = download.getSize();
			    	   String url = download.getUrl();
			    	   String date = download.getDate();
			    	   String dated = download.getDated();
			    	   String timed = download.getTimed();
                 
			    	   dou1.write(status);
					   dou1.writeUTF(filename);
					   dou1.writeUTF(filemenu);
					   dou1.writeLong(size);
					   dou1.writeUTF(url);
					   dou1.writeUTF(date);
					   if(treemodel != node3)
					   dou1.writeUTF(dated);
					   dou1.writeUTF(timed);
					   dou1.flush();
					   System.out.println(treemodel.name+"已写入");
			       }
			 } catch (FileNotFoundException e) {
					System.out.println("dakaiwenjianshipai");
	       } catch (IOException e) {
					System.out.println("duwenjianshipai");
					e.printStackTrace();
		   }
		 }
		 
		 //写树结点到文件
		 public void writeTreeNode(){
			try {
				BufferedWriter f2=new BufferedWriter(new FileWriter("doc/lishi.txt"));			
			  int i=0;
			try {
				for(i=0;i<leadNodeList.size();i++){
				   TreeModel treemodel = (TreeModel)leadNodeList.get(i);
				   String name = treemodel.name;
			       f2.write(i+name);
			       f2.newLine();
			       f2.write(treemodel.lujing);
			       write(treemodel,"doc/"+name+".txt");
			       f2.newLine();
			       f2.flush();
				}
				write(node2,"doc/已经下载.txt");
				write(node3,"doc/垃圾箱.txt");			
			} catch (FileNotFoundException e1) {
				System.out.println("文件打开失败");
			}
			   f2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		 }
		 
		 //读上一次没下载完的文件的信息
		 public void readNode1(){
			 try{
			 DataInputStream 
			 dis1 = new DataInputStream(
			 new BufferedInputStream(new FileInputStream("doc/正在下载.txt")));
			 int status2 = dis1.read();
			 System.out.println("status:"+status2);
			 for(int h=0;status2!=-1;h++){
				 String filename = dis1.readUTF();
				 String filemenu = dis1.readUTF();
				 long size = dis1.readLong();
				 String date = dis1.readUTF();
				 int lianjieleixing = dis1.read();
				 int nthread = 0;
				 long[] startPos = null;
				 long[] endPos = null;
				 long[] downloadSplitterdownloaded = null;
				 if(lianjieleixing == Download.HTTP){
				    nthread = dis1.read();
				    startPos = new long[nthread];
				    endPos = new long[nthread];
				    downloadSplitterdownloaded = new long[nthread];
				 for(int m=0;m<nthread;m++){
					 startPos[m] = dis1.readLong();
					 endPos[m] = dis1.readLong();
					 downloadSplitterdownloaded[m] = dis1.readLong();
				  }
				 }
				 String urlstr = dis1.readUTF();
				 String timed = dis1.readUTF();
                 Download download = new Download(status2,urlstr,filename,filemenu,nthread,startPos,
					   endPos,downloadSplitterdownloaded,size,date,timed,lianjieleixing);
                 node1.downloadList.add(download);
				 status2 = dis1.read();
			 }
			 } catch (FileNotFoundException e1) {
					System.out.println("文件打开失败");	 
			} catch (IOException e) {
				System.out.println("读文件失败");
				e.printStackTrace();
			}
		 }
		 
		 //写这次没下载完的文件的信息
		 public void writeNode1(){
			 try {
				DataOutputStream 
				 dou1 = new DataOutputStream(
							new BufferedOutputStream(new FileOutputStream("doc/正在下载.txt")));
				 for(int j=0;j<tablemodel.downloadingList.size();j++){			    	
			    	   Download download = (Download) tablemodel.downloadingList.get(j);
			    	   
			    	   int status = download.getStatus();
			    	   String filename = download.getFileName();
			    	   String filemenu = download.getMenuname();
			    	   long size = download.getSize();
			    	   String date = download.getDate();
			    	   int nthread = download.nthread;
			    	   String url = download.getUrl();
			    	   String timed = download.getTimed();
			    	 

⌨️ 快捷键说明

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