您好,登錄后才能下訂單哦!
首先看一下FTP軟件的整體代碼框架,具體內容如下
1、首先介紹程序的主入口FTPMain.java,采用了一個漂亮的外觀風格
package com.oyp.ftp; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.UIManager; import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel; public class FTPMain { /** * 本應用的程序入口 */ public static void main(String args[]) { //導致 runnable 的 run 方法在 EventQueue 的指派線程上被調用。 java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { //使用 LookAndFeel 對象設置當前的默認外觀。 UIManager.setLookAndFeel(new NimbusLookAndFeel());//設置一個非常漂亮的外觀 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); FTPClientFrame client_Frame = new FTPClientFrame(); client_Frame.setVisible(true); } catch (Exception ex) { Logger.getLogger(FTPClientFrame.class.getName()).log( Level.SEVERE, null, ex); } } }); } }
2、介紹界面的主程序代碼FTPClientFrame.java
package com.oyp.ftp; import java.awt.AWTException; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JSeparator; import javax.swing.JSplitPane; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.JToolBar; import javax.swing.UIManager; import com.oyp.ftp.panel.ftp.FtpPanel; import com.oyp.ftp.panel.local.LocalPanel; import com.oyp.ftp.panel.manager.FtpSiteDialog; import com.oyp.ftp.panel.queue.DownloadPanel; import com.oyp.ftp.panel.queue.QueuePanel; import com.oyp.ftp.panel.queue.UploadPanel; import com.oyp.ftp.utils.FtpClient; import com.oyp.ftp.utils.SiteInfoBean; import com.sun.java.swing.plaf.nimbus.*; public class FTPClientFrame extends javax.swing.JFrame { FtpClient ftpClient; private JPasswordField PassField; private JButton cutLinkButton; FtpPanel ftpPanel; LocalPanel localPanel; private JTextField portTextField; private JTextField serverTextField; private JTextField userTextField; private QueuePanel queuePanel; private UploadPanel uploadPanel; private DownloadPanel downloadPanel; private JSplitPane jSplitPane1; private JButton linkButton; private final LinkToAction LINK_TO_ACTION; // 連接到 按鈕的動作處理器 private final CutLinkAction CUT_LINK_ACTION; // 斷開 按鈕的動作處理器 private SystemTray systemTray; private JToggleButton shutdownButton; private final ImageIcon icon = new ImageIcon(getClass().getResource( "/com/oyp/ftp/res/trayIcon.png")); public FTPClientFrame() { LINK_TO_ACTION = new LinkToAction(this, "連接到", null); CUT_LINK_ACTION = new CutLinkAction(this, "斷開", null); initComponents(); initSystemTray(); } /** * 初始化系統托盤的方法 */ private void initSystemTray() { if (SystemTray.isSupported()) systemTray = SystemTray.getSystemTray(); TrayIcon trayIcon = new TrayIcon(icon.getImage()); PopupMenu popupMenu = new PopupMenu("托盤菜單"); // 創建顯示主窗體菜單項 MenuItem showMenuItem = new MenuItem("顯示主窗體"); showMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FTPClientFrame.this.setExtendedState(JFrame.NORMAL); FTPClientFrame.this.setVisible(true); } }); // 創建退出菜單項 MenuItem exitMenuItem = new MenuItem("退出"); exitMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); popupMenu.add(showMenuItem); popupMenu.addSeparator(); popupMenu.add(exitMenuItem); trayIcon.setPopupMenu(popupMenu); try { systemTray.add(trayIcon); } catch (AWTException e) { e.printStackTrace(); } } /** * 初始化程序界面的方法 */ private void initComponents() { setIconImage(icon.getImage()); java.awt.GridBagConstraints gridBagConstraints; JPanel jPanel1 = new JPanel(); JToolBar jToolBar1 = new JToolBar(); JButton linkTo = new JButton(); cutLinkButton = new JButton(); JPanel jPanel4 = new JPanel(); JLabel jLabel1 = new JLabel(); serverTextField = new JTextField(); JLabel jLabel2 = new JLabel(); userTextField = new JTextField(); JLabel jLabel3 = new JLabel(); PassField = new JPasswordField(); JLabel jLabel6 = new JLabel(); portTextField = new JTextField(); linkButton = new JButton(); JSplitPane jSplitPane2 = new JSplitPane(); jSplitPane1 = new JSplitPane(); ftpPanel = new FtpPanel(this); // 初始化FTP遠程資源面板 localPanel = new LocalPanel(this); // 初始化本地資源管理面板 uploadPanel = new UploadPanel(); // 初始化上傳隊列面板 downloadPanel = new DownloadPanel(); // 初始化下載隊列面板 queuePanel = new QueuePanel(this); // 初始化隊列面板 JTabbedPane jTabbedPane1 = new JTabbedPane(); JMenuBar MenuBar = new JMenuBar(); JMenu fileMenu = new JMenu(); JMenuItem ftpManageMenuItem = new JMenuItem(); JSeparator jSeparator1 = new JSeparator(); JMenuItem linkToMenuItem = new javax.swing.JMenuItem(); JMenuItem cutMenuItem = new javax.swing.JMenuItem(); JSeparator jSeparator2 = new javax.swing.JSeparator(); JMenuItem exitMenuItem = new javax.swing.JMenuItem(); JMenuItem uploadMenuItem = new javax.swing.JMenuItem(); JSeparator jSeparator3 = new javax.swing.JSeparator(); JMenuItem createFolderMenuItem = new javax.swing.JMenuItem(); JMenuItem renameMenuItem = new javax.swing.JMenuItem(); JMenuItem delMenuItem = new javax.swing.JMenuItem(); JMenu ftpMenu = new javax.swing.JMenu(); JMenuItem downMenuItem = new javax.swing.JMenuItem(); JSeparator jSeparator6 = new javax.swing.JSeparator(); JMenuItem ftpDelMenuItem = new javax.swing.JMenuItem(); JMenuItem ftpRenameMenuItem = new javax.swing.JMenuItem(); JMenuItem newFolderMenuItem = new javax.swing.JMenuItem(); JMenu helpMenu = new javax.swing.JMenu(); JMenuItem aboutMenuItem = new javax.swing.JMenuItem(); JMenuItem bugMenuItem = new javax.swing.JMenuItem(); // setTitle("基于Socket的FTP軟件Java實現"); setTitle("Java語言實現簡單FTP軟件__歐陽鵬設計"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } public void windowIconified(final WindowEvent e) { setVisible(false); } }); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent evt) { formComponentResized(evt); } }); getContentPane().setLayout(new java.awt.GridBagLayout()); jPanel1.setLayout(new java.awt.GridLayout(0, 1)); jToolBar1.setRollover(true); jToolBar1.setFloatable(false); linkTo.setText("連接到"); linkTo.setFocusable(false); linkTo.setAction(LINK_TO_ACTION); jToolBar1.add(linkTo); cutLinkButton.setText("斷開"); cutLinkButton.setEnabled(false); cutLinkButton.setFocusable(false); cutLinkButton.setAction(CUT_LINK_ACTION); jToolBar1.add(cutLinkButton); jPanel1.add(jToolBar1); shutdownButton = new JToggleButton(); shutdownButton.setText("自動關機"); shutdownButton.setToolTipText("隊列完成后,自動關閉計算機"); shutdownButton.setFocusable(false); jToolBar1.add(shutdownButton); jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.LINE_AXIS)); jLabel1.setText("主機地址:"); jPanel4.add(jLabel1); serverTextField.setText("192.168.1.100"); serverTextField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { LinkFTPKeyPressed(evt); } }); jPanel4.add(serverTextField); jLabel2.setText("用戶名:"); jPanel4.add(jLabel2); userTextField.setText("oyp"); userTextField.setMaximumSize(new java.awt.Dimension(200, 2147483647)); userTextField.setPreferredSize(new java.awt.Dimension(100, 21)); userTextField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { LinkFTPKeyPressed(evt); } }); jPanel4.add(userTextField); jLabel3.setText("密碼:"); jPanel4.add(jLabel3); PassField.setText("oyp"); PassField.setMaximumSize(new java.awt.Dimension(200, 2147483647)); PassField.setPreferredSize(new java.awt.Dimension(100, 21)); PassField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { LinkFTPKeyPressed(evt); } }); jPanel4.add(PassField); jLabel6.setText("端口:"); jPanel4.add(jLabel6); portTextField.setText("21"); portTextField.setMaximumSize(new java.awt.Dimension(100, 2147483647)); portTextField.setPreferredSize(new java.awt.Dimension(50, 21)); portTextField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { LinkFTPKeyPressed(evt); } }); jPanel4.add(portTextField); linkButton.setText("連接"); linkButton.setFocusable(false); linkButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); linkButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); linkButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { linkButtonActionPerformed(evt); } }); jPanel4.add(linkButton); jPanel1.add(jPanel4); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; //指定包含組件的顯示區域開始邊的單元格,其中行的第一個單元格為 gridx=0。 gridBagConstraints.gridy = 0; //指定位于組件顯示區域的頂部的單元格,其中最上邊的單元格為 gridy=0。 //當組件的顯示區域大于它所請求的顯示區域的大小時使用此字段。 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; //在水平方向而不是垂直方向上調整組件大小。 gridBagConstraints.weightx = 1.0; //指定如何分布額外的水平空間。 getContentPane().add(jPanel1, gridBagConstraints); jSplitPane2.setBorder(null); jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jSplitPane2.setResizeWeight(1.0); jSplitPane2.setContinuousLayout(true); jSplitPane1.setDividerLocation(400); jSplitPane1.setDividerSize(10); jSplitPane1.setOneTouchExpandable(true); jSplitPane1.setRightComponent(ftpPanel); jSplitPane1.setLeftComponent(localPanel); jSplitPane2.setLeftComponent(jSplitPane1); jTabbedPane1.setMinimumSize(new java.awt.Dimension(40, 170)); jTabbedPane1.addTab("隊列", queuePanel);// 添加隊列面板 jTabbedPane1.addTab("上傳隊列", uploadPanel);// 添加上傳面板 jTabbedPane1.addTab("下載隊列", downloadPanel);// 添加下載面板 jSplitPane2.setBottomComponent(jTabbedPane1); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; //在水平方向和垂直方向上同時調整組件大小。 gridBagConstraints.weightx = 1.0; //指定如何分布額外的水平空間。 gridBagConstraints.weighty = 1.0; //指定如何分布額外的垂直空間。 getContentPane().add(jSplitPane2, gridBagConstraints); fileMenu.setMnemonic('f'); fileMenu.setText("站點(F)"); ftpManageMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); ftpManageMenuItem.setText("FTP站點管理(S)"); ftpManageMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println("action"); FtpSiteDialog dialog = new FtpSiteDialog(FTPClientFrame.this); dialog.setVisible(true); } }); fileMenu.add(ftpManageMenuItem); fileMenu.add(jSeparator1); linkToMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK)); linkToMenuItem.setText("連接到...(C)"); linkToMenuItem.setAction(LINK_TO_ACTION); fileMenu.add(linkToMenuItem); cutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK)); cutMenuItem.setText("斷開(Z)"); cutMenuItem.setAction(CUT_LINK_ACTION); fileMenu.add(cutMenuItem); fileMenu.add(jSeparator2); exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK)); exitMenuItem.setText("退出(X)"); exitMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(exitMenuItem); MenuBar.add(fileMenu); JMenu localMenu = new JMenu(); localMenu.setMnemonic('l'); localMenu.setText("本地(L)"); uploadMenuItem.setMnemonic('U'); uploadMenuItem.setText("上傳(U)"); uploadMenuItem.setAction(localPanel.getActionMap().get("uploadAction")); localMenu.add(uploadMenuItem); localMenu.add(jSeparator3); createFolderMenuItem.setMnemonic('C'); createFolderMenuItem.setText("新建文件夾(C)"); createFolderMenuItem.setAction(localPanel.getActionMap().get( "createFolderAction")); localMenu.add(createFolderMenuItem); renameMenuItem.setMnemonic('R'); renameMenuItem.setText("重命名(R)"); renameMenuItem.setAction(localPanel.getActionMap().get("renameAction")); localMenu.add(renameMenuItem); delMenuItem.setMnemonic('D'); delMenuItem.setText("刪除(D)"); delMenuItem.setAction(localPanel.getActionMap().get("delAction")); localMenu.add(delMenuItem); JMenuItem localrefreshMenuItem = new JMenuItem(); localrefreshMenuItem.setMnemonic('R'); localrefreshMenuItem.setText("刷新(R)"); localrefreshMenuItem.setAction(localPanel.getActionMap().get( "refreshAction")); localMenu.add(localrefreshMenuItem); MenuBar.add(localMenu); ftpMenu.setMnemonic('r'); ftpMenu.setText("遠程(R)"); downMenuItem.setMnemonic('U'); downMenuItem.setText("下載(U)"); downMenuItem.setAction(ftpPanel.getActionMap().get("downAction")); ftpMenu.add(downMenuItem); ftpMenu.add(jSeparator6); ftpDelMenuItem.setMnemonic('D'); ftpDelMenuItem.setText("刪除(D)"); ftpDelMenuItem.setAction(ftpPanel.getActionMap().get("delAction")); ftpMenu.add(ftpDelMenuItem); ftpRenameMenuItem.setMnemonic('R'); ftpRenameMenuItem.setText("重命名(R)"); ftpRenameMenuItem .setAction(ftpPanel.getActionMap().get("renameAction")); ftpMenu.add(ftpRenameMenuItem); newFolderMenuItem.setMnemonic('C'); newFolderMenuItem.setText("新建文件夾(C)"); newFolderMenuItem.setAction(ftpPanel.getActionMap().get( "createFolderAction")); ftpMenu.add(newFolderMenuItem); JMenuItem refreshMenuItem = new JMenuItem(); refreshMenuItem.setMnemonic('R'); refreshMenuItem.setText("刷新(R)"); refreshMenuItem.setAction(ftpPanel.getActionMap().get("refreshAction")); ftpMenu.add(refreshMenuItem); MenuBar.add(ftpMenu); helpMenu.setText("幫助(H)"); aboutMenuItem.setMnemonic('a'); aboutMenuItem.setText("關于(A)"); aboutMenuItem.addActionListener(new AboutItemAction(this)); helpMenu.add(aboutMenuItem); bugMenuItem.setMnemonic('u'); bugMenuItem.setText("錯誤報告(U)"); bugMenuItem.addActionListener(new BugItemAction()); helpMenu.add(bugMenuItem); MenuBar.add(helpMenu); setJMenuBar(MenuBar); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit() .getScreenSize(); setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2, 800, 700); } public JToggleButton getShutdownButton() { return shutdownButton; } /** * 窗體裝載的事件處理方法 */ private void formWindowOpened(java.awt.event.WindowEvent evt) { jSplitPane1.setDividerLocation(0.50); localPanel.getLocalDiskComboBox().setSelectedIndex(1); localPanel.getLocalDiskComboBox().setSelectedIndex(0); } /** * 窗體大小調整的事件處理方法 */ private void formComponentResized(java.awt.event.ComponentEvent evt) { jSplitPane1.setDividerLocation(0.50); } /** * 連接按鈕的事件處理方法 */ private void linkButtonActionPerformed(java.awt.event.ActionEvent evt) { try { String server = serverTextField.getText(); // 獲取服務器地址 if (server == null) { return; } String portStr = portTextField.getText(); // 獲取端口號 if (portStr == null) { portStr = "21"; } int port = Integer.parseInt(portStr.trim()); String userStr = userTextField.getText(); // 獲取用戶名 userStr = userStr == null ? "" : userStr.trim(); String passStr = PassField.getText(); // 獲取密碼 passStr = passStr == null ? "" : passStr.trim(); cutLinkButton.doClick(); ftpClient = new FtpClient(); ftpClient.openServer(server.trim(), port); // 連接服務器 ftpClient.login(userStr, passStr); // 登錄服務器 ftpClient.binary(); // 使用二進制傳輸模式 if (ftpClient.serverIsOpen()) { // 如果連接成功 CUT_LINK_ACTION.setEnabled(true); // 設置斷開按鈕可用 } else { // 否則 CUT_LINK_ACTION.setEnabled(false); // 設置斷開按鈕不可用 return; // 并結束事件處理 } // 設置本地資源管理面板的FTP連接信息 localPanel.setFtpClient(server, port, userStr, passStr); // 設置上傳按鈕可用 localPanel.getActionMap().get("uploadAction").setEnabled(true); ftpPanel.setFtpClient(ftpClient);// 設置FTP資源管理面板的FTP連接信息 // 設置下載按鈕可用 ftpPanel.getActionMap().get("downAction").setEnabled(true); ftpPanel.refreshCurrentFolder();// 刷新FTP資源管理面板的當前文件夾 queuePanel.startQueue(); // 啟動任務隊列線程 } catch (Exception ex) { ex.printStackTrace(); } } /** * 連接FTP相關的文本框 和密碼框的回車事件 */ private void LinkFTPKeyPressed(java.awt.event.KeyEvent evt) { if (evt.getKeyChar() == '\n') { linkButton.doClick(); } } public LocalPanel getLocalPanel() { return localPanel; } public FtpPanel getFtpPanel() { return ftpPanel; } public QueuePanel getQueuePanel() { return queuePanel; } public UploadPanel getUploadPanel() { return uploadPanel; } public DownloadPanel getDownloadPanel() { return downloadPanel; } public FtpClient getFtpClient() { return ftpClient; } /** * 設置FTP連接信息的方法,由FTP站點管理器調用 */ public void setLinkInfo(SiteInfoBean bean) { serverTextField.setText(bean.getServer()); // 設置主機地址 portTextField.setText(bean.getPort() + ""); // 設置端口號 userTextField.setText(bean.getUserName()); // 設置用戶名 PassField.setText(""); // 密碼清空 PassField.requestFocus(); // 密碼框請求焦點 } }
整體界面如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。