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

📄 file_gui.java

📁 javaP2pgood.rar这个文件里面的代码我还没有看得懂
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import javax.swing.table.*;
import java.util.*;

/* When the Client selects a particular Listener's name from the list of the user's 
   connected and presses the button Open then this class is called. This class helps 
   the client directly connects to the Listener's machine and browse through, download, 
   upload, search for the files/folders shared by that particular Listener....
   
   This class provides the client facilities for download, upload, search for files,move 
   through shared folders facility...
*/


public class file_gui extends JFrame implements ActionListener
{
	private JButton file_open;				//
	private JButton file_search;			//	
	private JButton file_close;				//   
	private JButton file_upload;			//	 GUI Components for various purposes..
	private JButton file_download;			//
	private JTable file_listing;			//
	private JScrollPane file_scroller;		//
	private JLabel status;					//
		
	Container contentpane;					//  For the placeing the GUI Componments
	private Socket file_socket;				//  To Declare the client socket...
	Vector values = new Vector();			//  To declare a vector "values" for holding 
										    //	the results of the xml response from the 
										    //  the listener...
	
	TableModel default_table;				//	For JTable GUI Component
	String names[] = {"Files / Folders..", "Size", "Type"};// Name of the Columns in JTable
	Object data[][] ={{" "," "," " },{" ", " "," "},{" ", " "," "},{" ", " "," "},{" ", " "," "},{" ", " "," "}};// Initializing the JTable Columns..
	String ip_address;								// Variable to store the ip_address.
	String information[][];							
	String file_name= new String();
	String status_text;							// Variable to store the root information
	String flag_info;							// Variable to store the flag information
	boolean done =  false;

	/* Constructor is called with 4 parameters they are as follows...
	   1.  This is the result of XML parsing done (and stored in a vector) at the class
		   files which calls this class.
	   2.  The IP address of the Listener to which the client wants to connect...
	   3.  The root information (ie . the directory in which the user is currently 
		   browsing..). initially this information is kept as "ROOT".
	   4.  The flag Information about the directory in which the user5 is currently
		   browsing,this is used for upload purposes.. initially this is kept as 0 as 0 
		   stands for no uploads and client cannot upload on listener's root directory..	
    */
		   

	public file_gui(Vector parameter, String ip ,String stat_text, String flag_info)
	{
	 values = parameter;		// 
	 ip_address = ip;			//		Initializing the variables with parameters...
	 status_text = stat_text;	//	
	 this.flag_info = flag_info; // 	
	
        setTitle ("Peer 2 Peer Client");
		setSize(475,405);				// Set The size of the frame...
		
		contentpane = getContentPane();	//  Initialize the window for placing the 
										//  components..
		contentpane.setLayout(null);	// Setting the Layout to Absolute Layout..
		
		file_open = new JButton("Open");	// Initializing the GUI Component.
		file_open.setMnemonic('O');			// Setting the Mnemonic..
		file_open.setBounds(20,20,80,35);	// Positioning the GUI Component. 
		
		file_upload = new JButton("Upload");	// Initializing the GUI Component.
		file_upload.setMnemonic('U');			// Setting the Mnemonic..
		file_upload.setBounds(100,20,80,35);	// Positioning the GUI Component. 

		file_download = new JButton("Download");	// Initializing the GUI Component.
		file_download.setMnemonic('D');				// Setting the Mnemonic..
		file_download.setBounds(180,20,100,35);		// Positioning the GUI Component. 

		file_search = new JButton("Search");	// Initializing the GUI Component.
		file_search.setMnemonic('S');			// Setting the Mnemonic..
		file_search.setBounds(280,20,80,35);	// Positioning the GUI Component. 

		file_close = new JButton("Close");		// Initializing the GUI Component.
		file_close.setMnemonic('C');			// Setting the Mnemonic..
		file_close.setBounds(360,20,80,35);		// Positioning the GUI Component. 

		status = new JLabel(status_text);		// Initializing the GUI Component.
		status.setBounds(10,355,300,25);		// Positioning the GUI Component. 

		//	Initializing the Table
		
		default_table = new AbstractTableModel()
		{
        // These methods always need to be implemented.
        public int getColumnCount() { return names.length; }
	    public int getRowCount() { return data.length;}
        public Object getValueAt(int row, int col) {return data[row][col];}

	     // The default implementations of these methods in
         // AbstractTableModel would work, but we can refine them.
        public String getColumnName(int column) {return names[column];}
        public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
        public boolean isCellEditable(int row, int col) {return (col==4);}
        public void setValueAt(Object aValue, int row, int column) {
                data[row][column] = aValue;
				fireTableCellUpdated(row, column);
            }
         };

		/* This condition is applied so as to disable all the buttons except the close 
		   button when view files button is pressed as while viewing the files the client 
		   cannot make downloads/uploads etc...
        */

		 if (stat_text.equalsIgnoreCase("SEARCH RESULTS"))		
		 {
			file_open.setEnabled(false);
			file_download.setEnabled(false);
			file_upload.setEnabled(false);
			file_search.setEnabled(false);
		 }
			 
		contentpane.add(file_open);				//
		contentpane.add(file_close);			//
		contentpane.add(file_download);			//
		contentpane.add(file_upload);			//  Adding the GUI Components...
		contentpane.add(file_search);			//
		contentpane.add(status);				//
			
		/* An important function "formating" is called with the parameter a vector value
		   this helps in extracting the information stored in the vector and placing them 
		   in a string 2-Dimensional array in proper format for later reference also 
		   placing the information on to the JTable...
		*/   
	
		formating(values);
		
		// Positioning and Initializing the GUI Component (Table)... 

		file_listing = new JTable(default_table);
		file_listing.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//file_listing.getTableHeader().setReorderingAllowed(false);  
        //file_listing.setBounds(10,55,650,300);
		file_listing.setGridColor(new Color(255,255,255));
		//file_listing.setPreferredScrollableViewportSize(new Dimension(500, 70));

		// Adding Scroll Facility to the JTable by adding JScrollpane Component
			 
		file_scroller = new JScrollPane();//file_listing);
		file_scroller.setBounds(10,55,450,300);
		file_scroller.setViewportView(file_listing);
		file_scroller.setHorizontalScrollBarPolicy(                      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		file_listing.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
      
		contentpane.add(file_scroller);		// Add the Scroll Bar...

		// Adding the window  Listener to the gui window
		// ie. Code for the "cross"...

		addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
		   setVisible(false);
	 	}
        }
        );

		file_open.addActionListener(this);			//
		file_upload.addActionListener(this);		//	
		file_download.addActionListener(this);		//	Add the  ActionListener...
		file_search.addActionListener(this);		//	
		file_close.addActionListener(this);			//
	
	}			// End of Constructor file_gui...


// The part Below pertains to the Action Performed when a Button is pressed...

public void actionPerformed(ActionEvent ae)
  {
	if (ae.getSource() == file_open )	// When open Button is pressed...
	 {
		  /* This button will funbction only in case when the user selects a folder and 
			 opens it for knowing the details of the folder.. If the client tries to open 
			 a file a MessageBox is shown....
          */
		  
		  try
		  {
			 int row = 0;						
			 row = file_listing.getSelectedRow();		// Get the Selection...
											
			 if (information[row][2].equalsIgnoreCase("Folder")) // Check whether Folder
			 {
			   /* If a folder is selected and then pressed the open button then an object
			      of the class add_on is created (request) This is responsible for sending
				  the request to the Listener by using a function of the class of the name
				  "search_request". The parameters passed on to the function are the folder
				  name, followed by the search criteria here in this case it is *.*, foll-
				  -owed by the ip_address of the listener, followed by the flag 
				  information of the folder searched for...
			   */	  
			   add_on open_request = new add_on();
			   open_request.search_request(information[row][0],"*.*",ip_address,information[row][3]);	
			 }
			 else		// If a file is selected...
			 {
				// Alert / Message Box is displayed... 
				JOptionPane.showMessageDialog(this,"Cannot Open a File Over Network. Try Downloading it.  ","Peer 2 Peer...",JOptionPane.INFORMATION_MESSAGE);
			 }
	      }
		  catch(Exception es)
		  {}
	 }			// End File Open ....
	else if (ae.getSource() == file_close)	// If close button is pressed		
	 {
		this.setVisible(false);		// Hide this frame...	
	 }
	else if (ae.getSource() == file_download)	// If Download button is pressed...
	 {
		/* When download button is pressed a class called SwingWorker provided by Sun is
		   called this class helps in running a task in seperate thread thus helps in gui.
        */
		 final SwingWorker worker = new SwingWorker()
		  {
	 		public Object construct() 
			{
				// The function of the class is given a task to perform (downloading).
				// By calling a class donload_file..
				// This downloading is done in a seperate thread...
			   return new download_file();
			}
		  };
		 worker.start();	// Calling the start of the swingworker
								
	 }
	else if (ae.getSource() == file_upload)
	 {
	  	 done = false;
		 /* When Upload button is pressed a class called SwingWorker provided by Sun is
		   called this class helps in running a task in seperate thread thus helps in gui.
        */
		 final SwingWorker upload = new SwingWorker() 
		 {
			public Object construct()
			{
				// The function of the class is given a task to perform (Uploading).
				// By calling a class upload_file..
				// This uploading is done in a seperate thread...
			   return new upload_file();
			}
		 };
		upload.start();		// Calling the start of the swingworker
		if (done)
		 {
			this.setVisible(false);		// Hide the window after the upload...
		 }
	 } // End Upload.....
	else if (ae.getSource() == file_search)	// When Search Button is pressed...
	 {
		/* When search button is pressed by the client then root information is checked
		   if root information is is "ROOT" then a message box is shown indicating that
		   no search can be done...
		   else a class search screen is called... which caters to the search ...
		 */

		if (status_text.equalsIgnoreCase("Root"))	// If status_text is "ROOT"
		{
			JOptionPane.showMessageDialog(this,"Cannot Search on Root. Try searching in sub directories .  ","Peer 2 Peer...",JOptionPane.INFORMATION_MESSAGE);
		}
		else	// If search Text is not root...
		{
			/* Create an Object of the class search_screen ..
				the parameters passed on to the constructor are...
				1. Status_text...
				2. the ip address of the listener...
				3. The flag_info of the folder on which search is being performed...
			*/
			search_screen search_now = new search_screen(status_text,ip_address,flag_info);
			search_now.show();  // Show the frame...
		}
	 }
		

}			// End of Action Listener....

	

	/* An important function "formating" is called with the parameter a vector value
		   this helps in extracting the information stored in the vector and placing them 
		   in a string 2-Dimensional array in proper format for later reference also 
		   placing the information on to the JTable...
		*/   

 void formating(Vector values)
  {
		// To check whether the vector has more than one value or not.

   if (values.size() > 1)
	{
		int array_size = values.size();
					
		information = new String[array_size][4]; // Array in to which the information 
												 //  extracted is added... 
		// Information to be placed on the Table is put into the array (2-D) data...

⌨️ 快捷键说明

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