欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件本地窗口實(shí)現(xiàn)(5)

 更新時(shí)間:2017年03月31日 15:24:18   作者:歐陽(yáng)鵬  
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件,F(xiàn)TP軟件本地窗口的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家介紹了FTP軟件本地窗口的實(shí)現(xiàn)方法,供大家參考,具體內(nèi)容如下

1、首先看一下本地窗口的布局效果

2、看一下本地窗口實(shí)現(xiàn)的代碼框架

3、本地窗口的具體實(shí)現(xiàn)代碼LocalPanel.java

package com.oyp.ftp.panel.local; 
 
import java.awt.Color; 
import java.awt.Desktop; 
import java.awt.Dimension; 
import java.awt.event.ItemEvent; 
import java.io.File; 
import java.io.IOException; 
import java.util.Date; 
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.DefaultComboBoxModel; 
import javax.swing.JButton; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import javax.swing.table.TableRowSorter; 
import javax.swing.table.TableStringConverter; 
 
import com.oyp.ftp.FTPClientFrame; 
import com.oyp.ftp.panel.FTPTableCellRanderer; 
import com.oyp.ftp.panel.ftp.TableConverter; 
import com.oyp.ftp.utils.DiskFile; 
public class LocalPanel extends javax.swing.JPanel { 
 
  Queue<Object[]> queue = new LinkedList<Object[]>(); 
  private UploadThread uploadThread = null; 
  private Desktop desktop = null; 
  private javax.swing.JButton createFolderButton; 
  private javax.swing.JButton delButton; 
  private javax.swing.JScrollPane scrollPane; 
  private javax.swing.JToolBar.Separator jSeparator1; 
  private javax.swing.JToolBar toolBar; 
  private javax.swing.JComboBox localDiskComboBox; 
  javax.swing.JTable localDiskTable; 
  javax.swing.JLabel localSelFilePathLabel; 
  private javax.swing.JButton renameButton; 
  private javax.swing.JButton uploadButton; 
  private TableRowSorter<TableModel> sorter; 
  FTPClientFrame frame = null; 
 
  public LocalPanel() { 
    initComponents(); 
  } 
 
  public LocalPanel(FTPClientFrame client_Frame) { 
    frame = client_Frame; 
    if (Desktop.isDesktopSupported()) { 
      desktop = Desktop.getDesktop(); 
    } 
    initComponents(); 
  } 
 
  /** 
   * 界面布局與初始化方法 
   */ 
  private void initComponents() { 
    ActionMap actionMap = getActionMap(); 
    actionMap.put("delAction", new DelFileAction(this, "刪除", null)); 
    actionMap.put("renameAction", new RennameAction(this, "重命名", null)); 
    actionMap.put("createFolderAction", new CreateFolderAction(this, 
        "新建文件夾", null)); 
    actionMap.put("uploadAction", new UploadAction(this, "上傳", null)); 
    actionMap.put("refreshAction", new RefreshAction(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(); 
    uploadButton = new javax.swing.JButton(); 
    jSeparator1 = new javax.swing.JToolBar.Separator(); 
    localDiskComboBox = new javax.swing.JComboBox(); 
    localDiskComboBox.setPreferredSize(new Dimension(100, 25)); 
    scrollPane = new javax.swing.JScrollPane(); 
    localDiskTable = new javax.swing.JTable(); 
    localDiskTable.setDragEnabled(true); 
    localSelFilePathLabel = new javax.swing.JLabel(); 
    /** 
     * 向現(xiàn)有邊框添加一個(gè)標(biāo)題,使其具有指定的位置和默認(rèn)字體和文本顏色(由當(dāng)前外觀確定)。 
     * TitledBorder.CENTER: 將標(biāo)題文本置于邊框線的中心。 
     * TitledBorder.ABOVE_TOP: 將標(biāo)題置于邊框頂端線的上部。 
     */ 
    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.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); 
 
    uploadButton.setText("上傳"); 
    uploadButton.setFocusable(false); 
    uploadButton.setAction(actionMap.get("uploadAction")); 
    toolBar.add(uploadButton); 
 
    JButton refreshButton = new JButton(); 
    refreshButton.setText("刷新"); 
    refreshButton.setFocusable(false); 
    refreshButton.setAction(actionMap.get("refreshAction")); 
    toolBar.add(refreshButton); 
    toolBar.add(jSeparator1); 
     
    //File.listRoots():列出可用的文件系統(tǒng)根。 
    localDiskComboBox.setModel(new DefaultComboBoxModel(File.listRoots()));  
    localDiskComboBox.addItemListener(new java.awt.event.ItemListener() { 
      public void itemStateChanged(java.awt.event.ItemEvent evt) { 
        localDiskComboBoxItemStateChanged(evt); 
      } 
    }); 
    toolBar.add(localDiskComboBox); 
 
    gridBagConstraints = new java.awt.GridBagConstraints(); 
    gridBagConstraints.gridx = 0; 
    gridBagConstraints.gridy = 1; 
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
    gridBagConstraints.weightx = 1.0; 
    add(toolBar, gridBagConstraints); 
    localDiskTable.setModel(new LocalTableModel()); 
    localDiskTable.setShowHorizontalLines(false); 
    localDiskTable.setShowVerticalLines(false); 
    localDiskTable.getTableHeader().setReorderingAllowed(false); 
    localDiskTable.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
        localDiskTableMouseClicked(evt); 
      } 
    }); 
    scrollPane.setViewportView(localDiskTable); 
    scrollPane.getViewport().setBackground(Color.WHITE); 
    //設(shè)置渲染本地資源和FTP資源表格組件的渲染器 
    localDiskTable.getColumnModel().getColumn(0).setCellRenderer( 
        FTPTableCellRanderer.getCellRanderer()); 
    //RowSorter 的一個(gè)實(shí)現(xiàn),它使用 TableModel 提供排序和過(guò)濾操作。 
    sorter = new TableRowSorter<TableModel>(localDiskTable.getModel()); 
    TableStringConverter converter = new TableConverter(); 
    //設(shè)置負(fù)責(zé)將值從模型轉(zhuǎn)換為字符串的對(duì)象。 
    sorter.setStringConverter(converter); 
    //設(shè)置 RowSorter。RowSorter 用于提供對(duì) JTable 的排序和過(guò)濾。  
    localDiskTable.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); 
 
    localSelFilePathLabel.setBorder(javax.swing.BorderFactory 
        .createEtchedBorder()); 
    gridBagConstraints = new java.awt.GridBagConstraints(); 
    gridBagConstraints.gridx = 0; 
    gridBagConstraints.gridy = 3; 
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
    add(localSelFilePathLabel, gridBagConstraints); 
  } 
 
  /** 
   * 本地磁盤(pán)下拉選擇框的選項(xiàng)改變事件處理方法,由事件監(jiān)聽(tīng)器調(diào)用 
   */ 
  private void localDiskComboBoxItemStateChanged(java.awt.event.ItemEvent evt) { 
    if (evt.getStateChange() == ItemEvent.SELECTED) { 
      Object item = evt.getItem(); // 獲取選擇的下拉列表的選項(xiàng) 
      if (item instanceof File) { // 如果該選項(xiàng)是File類的實(shí)例對(duì)象 
        File selDisk = (File) item; // 將該選項(xiàng)轉(zhuǎn)換成File類 
        // 調(diào)用listLocalFiles()方法,顯示該File類指定的磁盤(pán)文件列表 
        listLocalFiles(selDisk); 
      } 
    } 
  } 
 
  /** 
   * 刷新指定文件夾的方法 
   */ 
  void refreshFolder(File file) { 
    listLocalFiles(file); 
  } 
 
  /** 
   * 刷新本地當(dāng)前文件夾的方法 
   */ 
  public void refreshCurrentFolder() { 
    final File file = getCurrentFolder(); // 獲取當(dāng)前文件夾 
    Runnable runnable = new Runnable() { // 創(chuàng)建新的線程 
      public void run() { 
        listLocalFiles(file); // 重載當(dāng)前文件夾的列表到表格中 
      } 
    }; 
    //導(dǎo)致 runnable 的 run 方法在 EventQueue 的指派線程上被調(diào)用。 
    SwingUtilities.invokeLater(runnable); // 在事件線程中調(diào)用該線程對(duì)象 
  } 
 
  /** 
   * 獲取當(dāng)前文件夾 
   */ 
  public File getCurrentFolder() { 
    // 使用路徑標(biāo)簽的路徑創(chuàng)建當(dāng)前文件夾對(duì)象 
    File file = new File(localSelFilePathLabel.getText()); 
    // 如果表格選擇了文件夾,或選擇的文件有真是的上級(jí)文件夾 
    if (localDiskTable.getSelectedRow() > 1 && file.getParentFile() != null) 
      file = file.getParentFile(); // 獲取該上級(jí)文件夾 
    return file; // 返回文件夾對(duì)象 
  } 
 
  /** 
   * 本地磁盤(pán)文件的表格單擊和雙擊事件處理方法 
   */ 
  private void localDiskTableMouseClicked(java.awt.event.MouseEvent evt) { 
    int selectedRow = localDiskTable.getSelectedRow(); // 獲取選擇的表格行號(hào) 
    if (selectedRow < 0) 
      return; 
    // 獲取表格中選擇的當(dāng)前行的第一個(gè)字段的值 
    Object value = localDiskTable.getValueAt(selectedRow, 0); 
    if (value instanceof DiskFile) { // 如果該值是DiskFile的實(shí)例對(duì)象 
      DiskFile selFile = (DiskFile) value; 
      // 設(shè)置狀態(tài)欄的本地文件路徑 
      localSelFilePathLabel.setText(selFile.getAbsolutePath()); 
      if (evt.getClickCount() >= 2) { // 如果是雙擊鼠標(biāo) 
        if (selFile.isDirectory()) { // 并且選擇的是文件夾 
          listLocalFiles(selFile); // 顯示該文件夾的內(nèi)容列表 
        } else if (desktop != null) { // 如果不是文件夾 
          try { 
            desktop.open(selFile); // 關(guān)聯(lián)本地系統(tǒng)程序打開(kāi)該文件 
          } catch (IOException ex) { 
            Logger.getLogger(FTPClientFrame.class.getName()).log( 
                Level.SEVERE, null, ex); 
          } 
        } 
      } 
    } else { // 如果選擇的表格內(nèi)容不是DiskFile類的實(shí)例 
      // 判斷選擇的是不是..選項(xiàng) 
      if (evt.getClickCount() >= 2 && value.equals("..")) { 
        // 創(chuàng)建當(dāng)前選擇文件的臨時(shí)文件 
        File tempFile = new File((localSelFilePathLabel.getText())); 
        // 顯示選擇的文件的上級(jí)目錄列表 
        listLocalFiles(tempFile.getParentFile()); 
      } 
    } 
  } 
 
  /** 
   * 讀取本地文件到表格的方法 
   */ 
  private void listLocalFiles(File selDisk) { 
    if (selDisk == null || selDisk.isFile()) { 
      return; 
    } 
    localSelFilePathLabel.setText(selDisk.getAbsolutePath()); 
    File[] listFiles = selDisk.listFiles(); // 獲取磁盤(pán)文件列表 
    // 獲取表格的數(shù)據(jù)模型 
    DefaultTableModel model = (DefaultTableModel) localDiskTable.getModel(); 
    model.setRowCount(0); // 清除模型的內(nèi)容 
    model.addRow(new Object[] { ".", "<DIR>", "" }); // 創(chuàng)建.選項(xiàng) 
    model.addRow(new Object[] { "..", "<DIR>", "" }); // 創(chuàng)建..選項(xiàng) 
    if (listFiles == null) { 
      JOptionPane.showMessageDialog(this, "該磁盤(pán)無(wú)法訪問(wèn)"); 
      return; 
    } 
    // 遍歷磁盤(pán)根文件夾的內(nèi)容,添加到表格中 
    for (File file : listFiles) { 
      File diskFile = new DiskFile(file); // 創(chuàng)建文件對(duì)象 
      String length = file.length() + "B "; // 獲取文件大小 
      if (file.length() > 1000 * 1000 * 1000) { // 計(jì)算文件G單位 
        length = file.length() / 1000000000 + "G "; 
      } 
      if (file.length() > 1000 * 1000) { // 計(jì)算文件M單位 
        length = file.length() / 1000000 + "M "; 
      } 
      if (file.length() > 1000) { 
        length = file.length() / 1000 + "K "; // 計(jì)算文件K單位 
      } 
      if (file.isDirectory()) { // 顯示文件夾標(biāo)志 
        length = "<DIR>"; 
      } 
      // 獲取文件的最后修改日期 
      String modifDate = new Date(file.lastModified()).toLocaleString(); 
      if (!file.canRead()) { 
        length = "未知"; 
        modifDate = "未知"; 
      } 
      // 將單個(gè)文件的信息添加到表格的數(shù)據(jù)模型中 
      model.addRow(new Object[] { diskFile, length, modifDate }); 
    } 
    localDiskTable.clearSelection(); // 取消表格的選擇項(xiàng) 
  } 
 
  /** 
   * 停止文件上傳線程的方法 
   */ 
  public void stopUploadThread() { 
    if (uploadThread != null) 
      uploadThread.stopThread(); 
  } 
 
  public javax.swing.JComboBox getLocalDiskComboBox() { 
    return localDiskComboBox; 
  } 
 
  /** 
   * 設(shè)置FTP連接,并啟動(dòng)上傳隊(duì)列線程的方法。 
   */ 
  public void setFtpClient(String server, int port, String userStr, 
      String passStr) { 
    if (uploadThread != null) 
      uploadThread.stopThread(); 
    uploadThread = new UploadThread(this, server, port, userStr, passStr); 
    uploadThread.start(); 
  } 
 
  public Queue<Object[]> getQueue() { 
    return queue; 
  } 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論