Java语言实现简单FTP软件 FTP软件主界面(4)
程序员文章站
2024-03-03 21:23:10
首先看一下ftp软件的整体代码框架,具体内容如下
1、首先介绍程序的主入口ftpmain.java,采用了一个漂亮的外观风格
package com...
首先看一下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(); // 密码框请求焦点 } }
整体界面如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android仿QQ6.0主页面侧滑效果
下一篇: 改变JAVA窗体属性的操作方法
推荐阅读
-
Java语言实现简单FTP软件 FTP上传下载队列窗口实现(7)
-
Java语言实现简单FTP软件 FTP软件本地窗口实现(5)
-
Java语言实现简单FTP软件 FTP软件主界面(4)
-
Java语言实现简单FTP软件 FTP上传下载管理模块实现(11)
-
Java语言实现简单FTP软件 辅助功能模块FTP站点管理实现(12)
-
Java语言实现简单FTP软件 FTP本地文件管理模块实现(9)
-
Java语言实现简单FTP软件 FTP远程文件管理模块实现(10)
-
Java语言实现简单FTP软件 FTP连接管理模块实现(8)
-
Java语言实现简单FTP软件 FTP软件效果图预览之上传功能(3)
-
Java语言实现简单FTP软件 FTP协议分析(1)