Java語言實現(xiàn)簡單FTP軟件 FTP軟件本地窗口實現(xiàn)(5)
本文為大家介紹了FTP軟件本地窗口的實現(xiàn)方法,供大家參考,具體內(nèi)容如下
1、首先看一下本地窗口的布局效果
2、看一下本地窗口實現(xiàn)的代碼框架
3、本地窗口的具體實現(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)有邊框添加一個標(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 的一個實現(xiàn),它使用 TableModel 提供排序和過濾操作。 sorter = new TableRowSorter<TableModel>(localDiskTable.getModel()); TableStringConverter converter = new TableConverter(); //設(shè)置負(fù)責(zé)將值從模型轉(zhuǎn)換為字符串的對象。 sorter.setStringConverter(converter); //設(shè)置 RowSorter。RowSorter 用于提供對 JTable 的排序和過濾。 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); } /** * 本地磁盤下拉選擇框的選項改變事件處理方法,由事件監(jiān)聽器調(diào)用 */ private void localDiskComboBoxItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { Object item = evt.getItem(); // 獲取選擇的下拉列表的選項 if (item instanceof File) { // 如果該選項是File類的實例對象 File selDisk = (File) item; // 將該選項轉(zhuǎn)換成File類 // 調(diào)用listLocalFiles()方法,顯示該File類指定的磁盤文件列表 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)用該線程對象 } /** * 獲取當(dāng)前文件夾 */ public File getCurrentFolder() { // 使用路徑標(biāo)簽的路徑創(chuàng)建當(dāng)前文件夾對象 File file = new File(localSelFilePathLabel.getText()); // 如果表格選擇了文件夾,或選擇的文件有真是的上級文件夾 if (localDiskTable.getSelectedRow() > 1 && file.getParentFile() != null) file = file.getParentFile(); // 獲取該上級文件夾 return file; // 返回文件夾對象 } /** * 本地磁盤文件的表格單擊和雙擊事件處理方法 */ private void localDiskTableMouseClicked(java.awt.event.MouseEvent evt) { int selectedRow = localDiskTable.getSelectedRow(); // 獲取選擇的表格行號 if (selectedRow < 0) return; // 獲取表格中選擇的當(dāng)前行的第一個字段的值 Object value = localDiskTable.getValueAt(selectedRow, 0); if (value instanceof DiskFile) { // 如果該值是DiskFile的實例對象 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)程序打開該文件 } catch (IOException ex) { Logger.getLogger(FTPClientFrame.class.getName()).log( Level.SEVERE, null, ex); } } } } else { // 如果選擇的表格內(nèi)容不是DiskFile類的實例 // 判斷選擇的是不是..選項 if (evt.getClickCount() >= 2 && value.equals("..")) { // 創(chuàng)建當(dāng)前選擇文件的臨時文件 File tempFile = new File((localSelFilePathLabel.getText())); // 顯示選擇的文件的上級目錄列表 listLocalFiles(tempFile.getParentFile()); } } } /** * 讀取本地文件到表格的方法 */ private void listLocalFiles(File selDisk) { if (selDisk == null || selDisk.isFile()) { return; } localSelFilePathLabel.setText(selDisk.getAbsolutePath()); File[] listFiles = selDisk.listFiles(); // 獲取磁盤文件列表 // 獲取表格的數(shù)據(jù)模型 DefaultTableModel model = (DefaultTableModel) localDiskTable.getModel(); model.setRowCount(0); // 清除模型的內(nèi)容 model.addRow(new Object[] { ".", "<DIR>", "" }); // 創(chuàng)建.選項 model.addRow(new Object[] { "..", "<DIR>", "" }); // 創(chuàng)建..選項 if (listFiles == null) { JOptionPane.showMessageDialog(this, "該磁盤無法訪問"); return; } // 遍歷磁盤根文件夾的內(nèi)容,添加到表格中 for (File file : listFiles) { File diskFile = new DiskFile(file); // 創(chuàng)建文件對象 String length = file.length() + "B "; // 獲取文件大小 if (file.length() > 1000 * 1000 * 1000) { // 計算文件G單位 length = file.length() / 1000000000 + "G "; } if (file.length() > 1000 * 1000) { // 計算文件M單位 length = file.length() / 1000000 + "M "; } if (file.length() > 1000) { length = file.length() / 1000 + "K "; // 計算文件K單位 } if (file.isDirectory()) { // 顯示文件夾標(biāo)志 length = "<DIR>"; } // 獲取文件的最后修改日期 String modifDate = new Date(file.lastModified()).toLocaleString(); if (!file.canRead()) { length = "未知"; modifDate = "未知"; } // 將單個文件的信息添加到表格的數(shù)據(jù)模型中 model.addRow(new Object[] { diskFile, length, modifDate }); } localDiskTable.clearSelection(); // 取消表格的選擇項 } /** * 停止文件上傳線程的方法 */ public void stopUploadThread() { if (uploadThread != null) uploadThread.stopThread(); } public javax.swing.JComboBox getLocalDiskComboBox() { return localDiskComboBox; } /** * 設(shè)置FTP連接,并啟動上傳隊列線程的方法。 */ 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Component和Configuration注解區(qū)別實例詳解
這篇文章主要為大家介紹了Component和Configuration注解區(qū)別實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11mybatis中查詢結(jié)果為空時不同返回類型對應(yīng)返回值問題
這篇文章主要介紹了mybatis中查詢結(jié)果為空時不同返回類型對應(yīng)返回值問題,本文分幾種方法給大家介紹的非常詳細,需要的朋友可以參考下2019-10-10Maven添加Tomcat插件實現(xiàn)熱部署代碼實例
這篇文章主要介紹了Maven添加Tomcat插件實現(xiàn)熱部署代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04IntelliJ?IDEA設(shè)置JVM運行參數(shù)的圖文介紹
這篇文章主要介紹了IntelliJ?IDEA設(shè)置JVM運行參數(shù)的方法,包括配置方式及優(yōu)先級,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04解決spring-cloud-config 多服務(wù)共享公共配置的問題
這篇文章主要介紹了解決spring-cloud-config 多服務(wù)共享公共配置的問題,本文通過多種方法給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Java 實戰(zhàn)項目錘煉之嘟嘟健身房管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個健身房管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11