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

📄 mainwindow.java

📁 一个简单的多线程下载程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    			
  	   			String name = fileChooser.getSelectedFile().getName();
  	   			currentIndex = name.charAt(4) - 48;
  	   			System.out.println("currentIndex:"+currentIndex);
  	   			
  	   			
  	   			
  	   			//读取文件------------------------------------------------------------
  				LoadFile load = new LoadFile(currentIndex);
    			load.loadInfo();
    			load.addToTabel();
			}
			
			if(temp.equals("退出"))
    		{
    			dispose();
    			exitSave();    	    
    			System.exit(0);
			}
						
			if(temp.equals("悬浮窗"))
    		{
    			if(logoFlag)
    			{
    				logoFlag = false;
    				logo.showVisible(logoFlag);
    			}
    			else
    			{
    				logoFlag = true;
    				logo.showVisible(logoFlag);
    			}
			}
			
			if(temp.equals("用户手册"))
    		{
    			Runtime r=Runtime.getRuntime(); 
				Process p=null; 
				String cmd[]={"notepad",".\\Text\\User"}; 
				try
				{ 
					p=r.exec(cmd); 
				} 
				catch(Exception e1)
				{ 
					System.out.println("error executing: "+cmd[0]); 
				}
			}
			
			if(temp.equals("关于QuickShare"))
    		{
    			Runtime r=Runtime.getRuntime(); 
				Process p=null; 
				String cmd[]={"notepad",".\\Text\\Readme"}; 
				try
				{ 
					p=r.exec(cmd); 
				} 
				catch(Exception e1)
				{ 
					System.out.println("error executing: "+cmd[0]); 
				}
			}
    	}
	}
	
	/**
	 *用于进行一些切换时必须进行的清理工作----------------------------------------------------
	 **/
	public void clearUp()
	{
		//将旧的数据存入文件中------------------------------------
        for(int i = 0;i < taskDownList.size();i++)
        {
        	TaskManage tempTaskManager = (TaskManage)taskDownList.get(i);
        	tempTaskManager.stopFlag = true;
        	tempTaskManager.changeFlag = true;
        	for(int j = 0;j < tempTaskManager.taskThreadList.size();j++)
        	{
        		((TaskThread)tempTaskManager.taskThreadList.get(j)).stopFlag = true;
        	}
        }
        
   		SaveTaskInfo saveInfo = new SaveTaskInfo(taskDownList,taskFinishedList,taskDeletedList,currentIndex);				
	    saveInfo.saveInfo();
    		    
	    /**
         *进行一些清理工作------------------------------------------------------------------
         **/
    	bottomCard.show(panelRightBottom,"default");
    		    
    	//清空表格信息----------------------------------------------------------------------
    	for(int i = 0;i < taskDownList.size();i++)
    	{
    		tableDownModel.removeRow(0);
    	}
    	for(int i = 0;i < taskFinishedList.size();i++)
    	{
    	   	tableFinishedModel.removeRow(0);
    	}
    	for(int i = 0;i < taskDeletedList.size();i++)
    	{
    	   	tableDeletedModel.removeRow(0);
    	}
    	//清空完毕---------------------------------------------------------------------------
    		    
    	//清空链表信息-----------------------------------------------------------------------
    	taskDownList.clear();
    	taskFinishedList.clear();
    	taskDeletedList.clear(); 
    	runningList.clear();
    	statusList.clear();
    		    
    	gridFlag = false;
		detailFlag = true;
		taskCount = 0;
	  		      
    	//清空链表信息结束-----------------------------------------------------------------------
    		    
    	/**
    	 *总体清理工作结束-------------------------------------------------------------------
    	 **/
   	}
   	
   	
   	
   	/**
   	 *系统退出时必须要做的清理事件-----------------------------------------------------
   	 **/
   	public void exitSave()
   	{
   		//在系统退出之前,将所有线程都停止,以防止数据丢失
        for(int i = 0;i < taskDownList.size();i++)
        {
        	TaskManage tempTaskManager = (TaskManage)taskDownList.get(i);
        	tempTaskManager.stopFlag = true;
        	for(int j = 0;j < tempTaskManager.taskThreadList.size();j++)
        	{
        		((TaskThread)tempTaskManager.taskThreadList.get(j)).stopFlag = true;
        	}
        }
        	
        SaveTaskInfo saveInfo = new SaveTaskInfo(taskDownList,taskFinishedList,taskDeletedList,currentIndex);				
    	saveInfo.saveInfo();
    	saveInfo.saveIndex();  	    
   	}
   	
   	/**
   	 *所有线程开始运行---------------------------------------------------------------------------
   	 **/
   	public void allRun()
   	{
   		for(int i = 0;i < taskDownList.size();i++)
    	{
    		//该任务是程序运行时的第一次运行,是用start而不是唤醒--------------------------------
        	if(!(Boolean)(statusList.get(i)))
        	{
        		statusList.remove(i);
				statusList.add(i,new Boolean(true));
				runningList.remove(i);
				runningList.add(i,new Boolean(true));
						
            	((TaskManage)taskDownList.get(i)).runSubThread();
            	((TaskManage)taskDownList.get(i)).start();

            }
            	
            else
            {
            	//该任务已在执行------------------------------------------------
				if((Boolean)(runningList.get(i)))
				{
					continue;
				}
			
				//该任务没有在执行,则执行----------------------------------------
            	else
            	{
            		((TaskManage)taskDownList.get(i)).threadNotify();
            		runningList.remove(i);
					runningList.add(i,new Boolean(true));
				}
				
            }
    	}
   	}
	
	/**
	 *表状组件事件侦听--------------------------------------------------------
	 **/
    class TableListener implements MouseListener
    {
    	//记录上一次鼠标双击事件对应的行---------------------
    	int preDoubleClick = 0;
    	int doubleClickNum = 0;
    	
    	public void mouseClicked(MouseEvent e)
    	{
    		JTable tempTable = new JTable();    		
    		
    		//监听下载表格事件----------------------------------------------------------
    		if(e.getSource()==tableDown)
    		{
    			tempTable = tableDown;
    			int i = tempTable.getSelectedRow();
    			
    			//鼠标点在空白处---------------------------------------------------------
    			if(i==-1)
    			{
    				bottomCard.show(panelRightBottom,"default");
    				tableDown.clearSelection();
    				System.out.println("the selection is:"+i);
    			}
    			
    			else
    			{
    				//单击鼠标事件-----------------------------------------------------------
					if(e.getClickCount()==1)
					{
						//int i = tempTable.getSelectedRow();
		
            			while(tableDownDetail.getRowCount()>0)
       					{
       						tableDownDetailModel.removeRow(0);
       					}
    					for(int j = 0;j < (((TaskManage)taskDownList.get(i)).taskThreadList).size();j++)
       					{
       						int threadNo = j+1;
       						String tempName = ((TaskManage)taskDownList.get(i)).fileName;
       						long tempStart = ((TaskThread)(((TaskManage)taskDownList.get(i)).taskThreadList.get(j))).finalStart;
	    					long tempEnd = ((TaskThread)(((TaskManage)taskDownList.get(i)).taskThreadList.get(j))).endPosition;
	    					long tempDown = ((TaskThread)((((TaskManage)taskDownList.get(i)).taskThreadList).get(j))).downLength;
	    					String tempURL = ((TaskManage)taskDownList.get(i)).url.toString();
				
							Object[] info = {threadNo,tempName,tempStart,tempEnd,tempDown,tempURL};
							tableDownDetailModel.addRow(info);
						}
						System.out.println("count:"+tableDownDetail.getRowCount());
						bottomCard.show(panelRightBottom,"down");
					}
			
					//鼠标双击事件---------------------------------------------------------------
            		if(e.getClickCount()==2)
           			{
            			int c = tempTable.getSelectedRow();
            			System.out.println("the Row index Selected:"+c);
            			System.out.println("the list name is:"+((TaskManage)taskDownList.get(c)).fileName);
            			//双击事件必然会发生单击事件----------------------------------------------
            			while(tableDownDetail.getRowCount()>0)
       					{
       						tableDownDetailModel.removeRow(0);
       					}
            			for(int j = 0;j < (((TaskManage)taskDownList.get(i)).taskThreadList).size();j++)
       					{
       						System.out.println("j:"+j);
       						int threadNo = j+1;
       						String tempName = ((TaskManage)taskDownList.get(i)).fileName;
       						long tempStart = ((TaskThread)(((TaskManage)taskDownList.get(i)).taskThreadList.get(j))).finalStart;
	    					long tempEnd = ((TaskThread)(((TaskManage)taskDownList.get(i)).taskThreadList.get(j))).endPosition;
	    					long tempDown = ((TaskThread)((((TaskManage)taskDownList.get(i)).taskThreadList).get(j))).downLength;
	    					String tempURL = ((TaskManage)taskDownList.get(i)).url.toString();
					
							Object[] info = {threadNo,tempName,tempStart,tempEnd,tempDown,tempURL};
							tableDownDetailModel.addRow(info);
						}
            	
            			//该任务是程序运行时的第一次运行,是用start而不是唤醒--------------------------------
            			if(!(Boolean)(statusList.get(i)))
            			{
            				statusList.remove(i);
							statusList.add(i,new Boolean(true));
							runningList.remove(i);
							runningList.add(i,new Boolean(true));
							
							tableDownModel.setValueAt("Downing",i,0);
            				((TaskManage)taskDownList.get(i)).runSubThread();
            				((TaskManage)taskDownList.get(i)).start();
            			
            				bottomCard.show(panelRightBottom,"down");
            			}
            	
            			else
            			{
            				//双击的任务正在执行,则停止------------------------------------------------
							if((Boolean)(runningList.get(i)))
							{
								((TaskManage)taskDownList.get(i)).stopFlag = true;
								runningList.remove(i);
								runningList.add(i,new Boolean(false));
							}
				
							//双击的任务没有在执行,则执行----------------------------------------
            				else
            				{
            					((TaskManage)taskDownList.get(i)).threadNotify();
            					runningList.remove(i);
								runningList.add(i,new Boolean(true));
							}
							bottomCard.show(panelRightBottom,"down");
            			}
           	 		}
    			}
    		
    		}
    		
    		//监听下载完成表格事件-------------------------------------------------------------------
    		else if(e.getSource()==tableFinished)
    		{
    			tempTable = tableFinished;
    			int i = tempTable.getSelectedRow();
    			
    			//鼠标点在空白处---------------------------------------------------------
    			if(i==-1)
    			{
    				bottomCard.show(panelRightBottom,"default");
    				tableFinished.clearSelection();
    			}
    		
				else
				{
       				String tempSave = ((TaskManage)taskFinishedList.get(i)).saveTo;
       				long tempLength = ((TaskManage)taskFinishedList.get(i)).fileLength;
       				long tempDown = ((TaskManage)taskFinishedList.get(i)).totalDownLength;
       				String tempURL = ((TaskManage)taskFinishedList.get(i)).url.toString();
       	
            		tableFinishedDetailModel.setValueAt("Yes",2,1);
            		tableFinishedDetailModel.setValueAt(tempSave,3,1);
            		tableFinishedDetailModel.setValueAt(tempLength,4,1);
            		tableFinishedDetailModel.setValueAt(tempDown,5,1);
            		tableFinishedDetailModel.setValueAt(tempURL,6,1);
            		
					bottomCard.show(panelRightBottom,"finished");
				}
    		}
    		
    		//监听已删除表格事件-------------------------------------------------------------------
    		else if(e.getSource()==tableDeleted)
    		{
    			tempTable = tableDeleted;
    			int i = tempTable.getSelectedRow();
    			
    			//鼠标点在空白处---------------------------------------------------------
    			if(i==-1)
    			{
    				bottomCard.show(panelRightBottom,"default");
    				tableDeleted.clearSelection();
    			}
    		
				else
				{
       				String tempSave = ((TaskManage)taskDeletedList.get(i)).saveTo;
       				long tempLength = ((TaskManage)taskDeletedList.get(i)).fileLength;
       				long tempDown = ((TaskManage)taskDeletedList.get(i)).totalDownLength;
       				String tempURL = ((TaskManage)taskDeletedList.get(i)).url.toString();
       	
            		tableDeletedDetailModel.setValueAt("Yes",2,1);
            		tableDeletedDetailModel.setValueAt(tempSave,3,1);
            		tableDeletedDetailModel.setValueAt(tempLength,4,1);
            		tableDeletedDetailModel.setValueAt(tempDown,5,1);
            		tableDeletedDetailModel.setValueAt(tempURL,6,1);
            		
					bottomCard.show(panelRightBottom,"deleted");
				}
    		}
    	}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    	public void mousePressed(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    }
}

⌨️ 快捷键说明

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