Home  >  Article  >  Java  >  Java language implementation of simple FTP software-FTP software remote window implementation (6)

Java language implementation of simple FTP software-FTP software remote window implementation (6)

黄舟
黄舟Original
2017-04-01 10:21:381309browse

这篇文章主要为大家详细介绍了Java语言实现简单FTP软件,FTP软件远程窗口的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家介绍了FTP软件远程窗口的实现方法,供大家参考,具体内容如下

1、首先看一下远程窗口的布局效果 

Java language implementation of simple FTP software-FTP software remote window implementation (6)

2、看一下本地窗口实现的代码框架

Java language implementation of simple FTP software-FTP software remote window implementation (6)

3、远程窗口主要实现代码FtpPanel.java

package com.oyp.ftp.panel.ftp; 
 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.util.LinkedList; 
import java.util.Queue; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
 
import javax.swing.ActionMap; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import javax.swing.table.TableRowSorter; 
import javax.swing.table.TableStringConverter; 
 
 
import sun.net.TelnetInputStream; 
import com.oyp.ftp.FTPClientFrame; 
import com.oyp.ftp.panel.FTPTableCellRanderer; 
import com.oyp.ftp.utils.FtpClient; 
import com.oyp.ftp.utils.FtpFile; 
 
public class FtpPanel extends javax.swing.JPanel { 
 
 FtpClient ftpClient; 
 private javax.swing.JButton createFolderButton; 
 private javax.swing.JButton delButton; 
 private javax.swing.JButton downButton; 
 javax.swing.JTable ftpDiskTable; 
 private javax.swing.JLabel ftpSelFilePathLabel; 
 private javax.swing.JScrollPane scrollPane; 
 private javax.swing.JToolBar toolBar; 
 private javax.swing.JButton refreshButton; 
 private javax.swing.JButton renameButton; 
 FTPClientFrame frame = null; 
 Queue queue = new LinkedList(); 
 private DownThread thread; 
 
 public FtpPanel() { 
 initComponents(); 
 } 
 
 public FtpPanel(FTPClientFrame client_Frame) { 
 frame = client_Frame; 
 initComponents(); 
 } 
 
 private void initComponents() { 
 ActionMap actionMap = getActionMap(); 
 actionMap.put("createFolderAction", new CreateFolderAction(this, 
  "创建文件夹", null)); 
 actionMap.put("delAction", new DelFileAction(this, "删除", null)); 
 actionMap.put("refreshAction", new RefreshAction(this, "刷新", null)); 
 actionMap.put("renameAction", new RenameAction(this, "重命名", null)); 
 actionMap.put("downAction", new DownAction(this, "下载", null)); 
 
 java.awt.GridBagConstraints gridBagConstraints; 
 
 toolBar = new javax.swing.JToolBar(); 
 delButton = new javax.swing.JButton(); 
 renameButton = new javax.swing.JButton(); 
 createFolderButton = new javax.swing.JButton(); 
 downButton = new javax.swing.JButton(); 
 refreshButton = new javax.swing.JButton(); 
 scrollPane = new JScrollPane(); 
 ftpDiskTable = new JTable(); 
 ftpDiskTable.setDragEnabled(true); 
 ftpSelFilePathLabel = new javax.swing.JLabel(); 
 
 setBorder(javax.swing.BorderFactory.createTitledBorder(null, "远程", 
  javax.swing.border.TitledBorder.CENTER, 
  javax.swing.border.TitledBorder.ABOVE_TOP)); 
 setLayout(new java.awt.GridBagLayout()); 
 
 toolBar.setRollover(true); 
 toolBar.setFloatable(false); 
 
 delButton.setText("删除"); 
 delButton.setFocusable(false); 
 delButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 
 delButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
 delButton.setAction(actionMap.get("delAction")); 
 toolBar.add(delButton); 
 
 renameButton.setText("重命名"); 
 renameButton.setFocusable(false); 
 renameButton.setAction(actionMap.get("renameAction")); 
 toolBar.add(renameButton); 
 
 createFolderButton.setText("新文件夹"); 
 createFolderButton.setFocusable(false); 
 createFolderButton.setAction(actionMap.get("createFolderAction")); 
 toolBar.add(createFolderButton); 
 
 downButton.setText("下载"); 
 downButton.setFocusable(false); 
 downButton.setAction(actionMap.get("downAction")); 
 toolBar.add(downButton); 
 
 refreshButton.setText("刷新"); 
 refreshButton.setFocusable(false); 
 refreshButton.setAction(actionMap.get("refreshAction")); 
 toolBar.add(refreshButton); 
 
 gridBagConstraints = new java.awt.GridBagConstraints(); 
 gridBagConstraints.gridx = 0; 
 gridBagConstraints.gridy = 0; 
 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
 gridBagConstraints.weightx = 1.0; 
 add(toolBar, gridBagConstraints); 
 
 ftpDiskTable.setModel(new FtpTableModel()); 
 ftpDiskTable.setShowHorizontalLines(false); 
 ftpDiskTable.setShowVerticalLines(false); 
 ftpDiskTable.getTableHeader().setReorderingAllowed(false); 
 ftpDiskTable.setDoubleBuffered(true); 
 ftpDiskTable.addMouseListener(new java.awt.event.MouseAdapter() { 
  public void mouseClicked(java.awt.event.MouseEvent evt) { 
  ftpDiskTableMouseClicked(evt); 
  } 
 }); 
 scrollPane.setViewportView(ftpDiskTable); 
 scrollPane.getViewport().setBackground(Color.WHITE); 
 //设置渲染本地资源和FTP资源表格组件的渲染器 
 ftpDiskTable.getColumnModel().getColumn(0).setCellRenderer( 
  FTPTableCellRanderer.getCellRanderer()); 
 //RowSorter 的一个实现,它使用 TableModel 提供排序和过滤操作。 
 TableRowSorter sorter = new TableRowSorter( 
  ftpDiskTable.getModel()); 
 TableStringConverter converter = new TableConverter(); 
 //设置负责将值从模型转换为字符串的对象。 
 sorter.setStringConverter(converter); 
 //设置 RowSorter。RowSorter 用于提供对 JTable 的排序和过滤。 
 ftpDiskTable.setRowSorter(sorter); 
 /** 
  * 颠倒指定列的排序顺序。调用此方法时,由子类提供具体行为。 
  * 通常,如果指定列已经是主要排序列,则此方法将升序变为降序(或将降序变为升序); 
  * 否则,使指定列成为主要排序列,并使用升序排序顺序。如果指定列不可排序,则此方法没有任何效果。 
  */ 
 sorter.toggleSortOrder(0); 
 
 gridBagConstraints = new java.awt.GridBagConstraints(); 
 gridBagConstraints.gridx = 0; 
 gridBagConstraints.gridy = 2; 
 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
 gridBagConstraints.weightx = 1.0; 
 gridBagConstraints.weighty = 1.0; 
 add(scrollPane, gridBagConstraints); 
 
 ftpSelFilePathLabel.setBorder(javax.swing.BorderFactory 
  .createEtchedBorder()); 
 gridBagConstraints = new java.awt.GridBagConstraints(); 
 gridBagConstraints.gridx = 0; 
 gridBagConstraints.gridy = 3; 
 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
 add(ftpSelFilePathLabel, gridBagConstraints); 
 } 
 
 /** 
 * 表格单击或双击事件的处理方法。 
 */ 
 private void ftpDiskTableMouseClicked(java.awt.event.MouseEvent evt) { 
 int selectedRow = ftpDiskTable.getSelectedRow(); 
 Object value = ftpDiskTable.getValueAt(selectedRow, 0); 
 if (value instanceof FtpFile) { 
  FtpFile selFile = (FtpFile) value; 
  ftpSelFilePathLabel.setText(selFile.getAbsolutePath()); 
  if (evt.getClickCount() >= 2) { //双击鼠标 
  if (selFile.isDirectory()) { 
   try { 
   ftpClient.cd(selFile.getAbsolutePath()); 
   listFtpFiles(ftpClient.list()); 
   } catch (IOException ex) { 
   ex.printStackTrace(); 
   } 
  } 
  } 
 } 
 } 
 
 /** 
 * 读取FTP文件到表格的方法 
 * @param list 
 *  读取FTP服务器资源列表的输入流 
 */ 
 public synchronized void listFtpFiles(final TelnetInputStream list) { 
 // 获取表格的数据模型 
 final DefaultTableModel model = (DefaultTableModel) ftpDiskTable 
  .getModel(); 
 model.setRowCount(0); 
 // 创建一个线程类 
 Runnable runnable = new Runnable() { 
  public synchronized void run() { 
  ftpDiskTable.clearSelection(); 
  try { 
   String pwd = getPwd(); // 获取FTP服务器的当前文件夹 
   model.addRow(new Object[] { new FtpFile(".", pwd, true), 
    "", "" }); // 添加“.”符号 
   model.addRow(new Object[] { new FtpFile("..", pwd, true), 
    "", "" }); // 添加“..”符号 
   /* 
   byte[]names=new byte[2048]; 
   int bufsize=0; 
   bufsize=list.read(names, 0, names.length); 
//   list.close(); 
   int i=0,j=0; 
   while(i
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn