首先看一下ftp软件的整体代码框架,具体内容如下
1、首先介绍程序的主入口ftpmain.java,采用了一个漂亮的外观风格
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
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(); // 密码框请求焦点 } } |
整体界面如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/ouyang_peng/article/details/9672939