📄 frame1.java~216~
字号:
package ftpclient;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import java.io.*;
import sun.net.TelnetInputStream;
import sun.net.ftp.*;
import java.lang.Object;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.tree.TreePath;
import sun.net.TelnetOutputStream;
import java.util.Properties;
/**
* <p>Title: an example</p>
* <p>Description:an no</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: home</p>
* @author liujun
* @version 1.0
*/
public class Frame1 extends JFrame {
private JPanel contentPane;
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JCheckBox jCheckbox1 = new JCheckBox();
FtpClient ftp=null;
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
PaneLayout paneLayout1 = new PaneLayout();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField jTextField4 = new JTextField();
JLabel jLabel4 = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
Box box1;
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
BorderLayout borderLayout2 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JScrollPane jScrollPane2 = new JScrollPane();
//定义树节点,模型和树视图
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("目录中没有文件");
DefaultTreeModel model2 = new DefaultTreeModel(root2);
JTree jTree2 = new JTree(model2);
JButton jButton5 = new JButton();
JLabel statusLabel = new JLabel();
List list1 = new List();
JScrollPane jScrollPane3 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
box1 = Box.createVerticalBox();
jTextField1.setText("192.168.101.2");
contentPane.setLayout(paneLayout1);
this.setSize(new Dimension(487, 462));
this.setTitle("文件传输系统");
jTextField2.setText("anonymous");
jTextField3.setText("a");
jCheckbox1.setText("使用匿名");
jCheckbox1.setSelected(true);
jPanel1.setLayout(xYLayout1);
jLabel1.setText("服务器地址:");
jLabel2.setText("用户名:");
jLabel3.setText("密 码:");
jTextField4.setText("21");
jLabel4.setText("端口号:");
jPanel2.setLayout(borderLayout1);
jPanel3.setLayout(xYLayout2);
jPanel4.setDebugGraphicsOptions(0);
jPanel4.setMinimumSize(new Dimension(160, 18));
jPanel4.setPreferredSize(new Dimension(160, 18));
jPanel4.setLayout(borderLayout2);
jButton3.setText("文件上载");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
jButton4.setText("文件下载");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton4_actionPerformed(e);
}
});
box1.setEnabled(true);
jPanel3.setDebugGraphicsOptions(0);
jPanel3.setMinimumSize(new Dimension(400, 240));
jPanel3.setPreferredSize(new Dimension(400, 240));
jTree2.setToggleClickCount(2);
jButton5.setText("选择目录");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton5_actionPerformed(e);
}
});
statusLabel.setText("没有连接ftp服务器");
list1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
list1_mouseClicked(e);
}
});
jTextArea1.setText(" ");
jButton1.setText("连接到服务器");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("断开服务器连接");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
list1.setMultipleMode(true);
jLabel5.setText("^^FTP服务器目录列表^^");
jLabel6.setText("^^本地硬盘目录列表^^");
jPanel1.add(jTextField1, new XYConstraints(81, 11, 297, -1));
jPanel1.add(jLabel1, new XYConstraints(21, 11, -1, -1));
jPanel1.add(statusLabel, new XYConstraints(-2, 370, 484, 29));
jPanel1.add(jButton2, new XYConstraints(34, 304, 122, -1));
jPanel1.add(jButton1, new XYConstraints(34, 268, 106, 23));
jPanel1.add(jLabel4, new XYConstraints(22, 50, -1, -1));
jPanel1.add(jTextField4, new XYConstraints(82, 49, 88, -1));
jPanel1.add(jCheckbox1, new XYConstraints(22, 84, -1, -1));
jPanel1.add(jTextField2, new XYConstraints(80, 124, 86, -1));
jPanel1.add(jLabel2, new XYConstraints(20, 124, 58, 21));
jPanel1.add(jTextField3, new XYConstraints(80, 165, 87, -1));
jPanel1.add(jLabel3, new XYConstraints(20, 166, -1, -1));
jPanel2.add(box1, BorderLayout.CENTER);
jPanel3.add(jScrollPane2, new XYConstraints(295, 6, 173, 280));
jPanel3.add(jScrollPane1, new XYConstraints(7, 6, 194, 281));
jPanel3.add(jButton4, new XYConstraints(205, 93, 84, 19));
jPanel3.add(jButton3, new XYConstraints(205, 53, 86, 19));
jPanel3.add(jButton5, new XYConstraints(205, 136, 85, 19));
jPanel3.add(jLabel5, new XYConstraints(16, 293, 144, 22));
jPanel3.add(jLabel6, new XYConstraints(314, 293, 144, 22));
jScrollPane1.getViewport().add(list1, null);
jScrollPane2.getViewport().add(jTree2, null);
box1.add(jPanel3, null);
box1.add(jPanel4, null);
jPanel4.add(jScrollPane3, BorderLayout.CENTER);
jScrollPane3.getViewport().add(jTextArea1, null);
myInit();
jTabbedPane1.add(jPanel2, "文件处理");
jTabbedPane1.add(jPanel1, "连接FTP服务器");
contentPane.add(jTabbedPane1, new PaneConstraints("jTabbedPane1", "jTabbedPane1", PaneConstraints.ROOT, 0.5f));
}
//---------------------------------------------------------------------------
// 初始化程序: private void myInit()
// 作用:对list1和jTree1进行初始化
private void myInit() {
File rootfile= new File("c:\\");//得到根目录文件
if(rootfile.isFile()) rootfile=rootfile.getParentFile();//如果得到的不是目录,则使用他的目录
DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(rootfile.getPath());
setTree(rootfile.getPath(),rootTree2);//遍历目录树
model2.setRoot(rootTree2);//设置模型的根节点
model2.reload();//重新构造树视图
if(ftp!=null) {
ReloadList();
jTextArea1.append(ftp.welcomeMsg);
}
}
//---------------------------------------------------------------------------
// 浏览程序: private void ReloadList()
// 作用:清空目录列表,调用List()方法获取文件列表。
private void ReloadList(){
StringBuffer buf=new StringBuffer();
int ch;
//清空目录列表
list1.removeAll();
try {
//调用List()方法得到目录表
TelnetInputStream t = ftp.list();
t.setStickyCRLF(true);
//分解TelnetInputStream数据流
while ( (ch = t.read()) >= 0) {
if (ch == '\n') {
//向列表框添加分解得到的目录和文件
list1.add(getDIR(buf.toString()));
buf.setLength(0);
}
else {
buf.append( (char) ch);
}
}
//完成后关闭TelnetInputStream数据流
t.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
//刷新列表框的内容
list1.validate();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
//---------------------------------------------------------------------------
// 浏览程序: public String getDIR(String path)
// 作用:分解字符串得到目录和文件名
public String getDIR(String path){
String DIRName;
int ch;
//分解字符串得到目录和文件名
int begin=55;
DIRName=path.substring(begin).trim();
return DIRName;
}
//---------------------------------------------------------------------------
// 浏览程序: public void setTree(String Path,DefaultMutableTreeNode TreePath)
// 作用:构造jTree对象的目录树
public void setTree(String Path,DefaultMutableTreeNode TreePath){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -