附录2: 主要源程序
(1) 主界面类Ftp.java
public class Ftp extends JFrame {
FtpBean ftp=new FtpBean(); //FTP客户端对象 private FileWriter write ; private PrintWriter out; private Date time;
private JTextField url = new JTextField(); //FTP服务器地址 private JTextField port = new JTextField(); //FTP服务器端口 private JTextField username = new JTextField(); //用户名 private JTextField password = new JTextField(); //密码 private JTextField status = new JTextField(); //当前状态 private JButton connectButton=new JButton(\连接\); //连接按钮 private JButton closeButton=new JButton(\断开连接\); //断开连接按钮 private JPopupMenu popup = new JPopupMenu();//建立一个弹出式菜单 private JMenuItem refresh = new JMenuItem(\刷新\); //右键弹出菜单 private JMenuItem delete = new JMenuItem(\删除\); private JMenuItem download = new JMenuItem(\下载\); private JMenuItem upload = new JMenuItem(\上传\); private JMenuItem rename = new JMenuItem(\重命名\); private JMenuItem upDir = new JMenuItem(\上级目录\);
private JMenuItem showLog = new JMenuItem(\显示日志信息\);
final DefaultListModel m=new DefaultListModel(); //显示文件列表 JList fileList = new JList(m); //设置一组数组 JScrollPane fileScrollPane;
private Dimension faceSize = new Dimension(400, 400); //框架大小 public Ftp() {
init();
createPopup(); //弹出菜单 time = new Date(); try {
write = new FileWriter(\,true); out = new PrintWriter(write,true); }
catch(IOException ioe) {
ioe.printStackTrace(); }
status.setEditable(false);
this.add(status,BorderLayout.SOUTH); //放置操作信息显示板
connectButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
connect(); } });
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
time = new Date();
out.println(time+\离开服务器:\+url.getText()); } });
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加框架的关闭事件处理 this.pack();
this.setSize(faceSize); //设置运行时窗口的位置
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation( (int) (screenSize.width - faceSize.getWidth()) / 2, (int) (screenSize.height - faceSize.getHeight()) / 2); this.setResizable(false);
this.setTitle(\简单的FTP客户端\); //设置标题 Image i=Toolkit.getDefaultToolkit().getImage(\); this.setIconImage(i); setVisible(true);
this.addWindowListener( //关闭程序时的操作 new WindowAdapter() {
public void windowClosing(WindowEvent e) {
close();
System.exit(0); } }
); }
void init() {
Container contentPane = getContentPane(); //获取面板 contentPane.setLayout(new BorderLayout()); //设置布局
JPanel up = new JPanel(); //设置面板上方控件布局 up.setLayout(new GridLayout(3,2)); url.setText(\); port.setText(\);
username.setText(\);
password.setText(\);
JPanel no1 = new JPanel(); no1.setLayout(new BorderLayout());
no1.add(new JLabel(\服务器:\),BorderLayout.WEST); no1.add(url,BorderLayout.CENTER);
JPanel no2 = new JPanel(); no2.setLayout(new BorderLayout());
no2.add(new JLabel(\端口号:\),BorderLayout.WEST); no2.add(port,BorderLayout.CENTER);
JPanel no3 = new JPanel(); no3.setLayout(new BorderLayout());
no3.add(new JLabel(\用户名:\),BorderLayout.WEST); no3.add(username,BorderLayout.CENTER);
JPanel no4 = new JPanel(); no4.setLayout(new BorderLayout());
no4.add(new JLabel(\密码 :\),BorderLayout.WEST); no4.add(password,BorderLayout.CENTER); up.add(no1); up.add(no2); up.add(no3); up.add(no4);
up.add(connectButton); up.add(closeButton);
closeButton.setEnabled(false);
this.add(up,BorderLayout.NORTH); //上方 fileScrollPane = new JScrollPane(fileList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
fileScrollPane.setPreferredSize(new Dimension(300,400)); this.add(fileScrollPane,BorderLayout.CENTER); //中部 }
void createPopup()
{
refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update(); } });
popup.add(refresh);
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteFile(); } });
popup.add(delete);
download.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
downloadFile(); } });
popup.add(download);
upload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
uploadFile(); } });
popup.add(upload);
rename.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
renameFile(); } });
popup.add(rename);