这对某人来说应该是一个简单的问题。 我有一个应用程序需要以树形形式显示用户文件系统和 FTP 站点。 我可以列出这些。
问题。 我正在使用 Swing Jtree 控件,但找不到引用它的方法。
我是 Java 新手,但不是编程新手。 我试图获得对控件的引用,但似乎没有办法。 以编程方式创建一个是可行的方法吗?
package FTPTESTALPHA.TheTest;
import java.awt.EventQueue;
import java.util.*;
import java.util.Locale.Category;
import java.util.stream.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
import java.io.PrintWriter;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
public class Form1 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private static JTextField txtAddress;
private static JTextField txtUserName;
private static JPasswordField pasPassword;
//private DefaultMutableTreeNode root;
//private DefaultTreeModel treeModel;
//private JTree tree;
//private DefaultMutableTreeNode rootNode;
// private static int length;
//private final DefaultMutableTreeNode rootNode;
/**.
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Form1 frame = new Form1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Form1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 872, 545);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblAddress = new JLabel("Server Address ");
lblAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
lblAddress.setBounds(10, 10, 105, 20);
contentPane.add(lblAddress);
txtAddress = new JTextField();
// txtAddress.setText();
txtAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
txtAddress.setBounds(20, 40, 202, 19);
contentPane.add(txtAddress);
txtAddress.setColumns(10);
JLabel lblUserName = new JLabel("User Name");
lblUserName.setFont(new Font("Tahoma", Font.BOLD, 12));
lblUserName.setBounds(256, 10, 105, 18);
contentPane.add(lblUserName);
txtUserName = new JTextField();
txtUserName.setFont(new Font("Tahoma", Font.BOLD, 12));
txtUserName.setBounds(256, 35, 96, 19);
contentPane.add(txtUserName);
txtUserName.setColumns(10);
JLabel lblPass = new JLabel("Password");
lblPass.setFont(new Font("Tahoma", Font.BOLD, 12));
lblPass.setBounds(387, 10, 105, 20);
contentPane.add(lblPass);
pasPassword = new JPasswordField();
pasPassword.setBounds(387, 34, 136, 20);
contentPane.add(pasPassword);
JButton btnConnect = new JButton("Connect");
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean dis = false;
MakeConnection(dis);
}
});
btnConnect.setFont(new Font("Tahoma", Font.BOLD, 14));
btnConnect.setBounds(563, 22, 105, 33);
contentPane.add(btnConnect);
JButton btnDisconnect = new JButton("Disconnect");
btnDisconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean dis = true;
MakeConnection(dis);
}
});
btnDisconnect.setFont(new Font("Tahoma", Font.BOLD, 14));
btnDisconnect.setBounds(692, 22, 136, 33);
contentPane.add(btnDisconnect);
JTree treeLocal = new JTree();
treeLocal.setFont(new Font("Tahoma", Font.BOLD, 12));
treeLocal.setBounds(10, 83, 311, 291);
contentPane.add(treeLocal);
JTree treeRemote = new JTree();
treeRemote.setFont(new Font("Tahoma", Font.BOLD, 12));
treeRemote.setBounds(514, 83, 334, 291);
contentPane.add(treeRemote);
}
//private final DefaultMutableTreeNode rootNode;
public void MakeConnection(boolean dis) {
FTPClient ftpClient = new FTPClient();
try {
// Connect to the FTP server
String FTP_SERVER = txtAddress.getText();
int FTP_PORT = 21;
String FTP_USER= txtUserName.getText();
String FTP_PASSWORD= pasPassword.getText();
System.setProperty(FTPClient.FTP_SYSTEM_TYPE_DEFAULT, FTPClientConfig.SYST_UNIX);
boolean result ;
ftpClient. connect(FTP_SERVER);
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
ftpClient.addProtocolCommandListener(
new PrintCommandListener(
new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));
ftpClient.enterLocalPassiveMode();
result = ftpClient.login(FTP_USER, FTP_PASSWORD);
System.out.println(result);
String Path = "/";
// boolean success = ftpClient.changeWorkingDirectory(Path);
listTheFiles(ftpClient, Path);
// scanDir(ftpClient, Path);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (dis) {
BreakConnection(ftpClient);
}
}
}
public static void BreakConnection(FTPClient ftpClient) {
//FTPClient ftpClient = new FTPClient();
try {
// Disconnect from the FTP server
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
System.out.println("Disconnected");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void listTheFiles(FTPClient ftpClient, String Path ) throws IOException {
System.out.println("Listing files on the FTP server:");
// ftpClient.setAutodetectUTF8(isDefaultLookAndFeelDecorated());
if(ftpClient.isConnected()) {
FTPFile[] myDirs = ftpClient.listDirectories();
FTPFile[] files = ftpClient.listFiles(Path) ;
for (int i = 0; i < myDirs.length; i++) {
System.out.println(myDirs[i]);
}
popTree(myDirs);}
}
@SuppressWarnings("unused")
public void popTree(FTPFile[] dirs) {
//DefaultMutableTreeNode root = new DefaultMutableTreeNode("\\");
DefaultMutableTreeNode TheRoot = new DefaultMutableTreeNode();
//TheRoot.isRoot();
for(int i =0; i< dirs.length; i++) {
TheRoot.add(new DefaultMutableTreeNode(dirs[i]));
}
//root.add(new DefaultMutableTreeNode("A"));
// root.add(new DefaultMutableTreeNode("B"));
//a root.add(new DefaultMutableTreeNode("C"));
JTree treeLocal = new JTree(TheRoot);
}
/*
* public static DefaultMutableTreeNode treeify(FTPFile[][] paths) {
* DefaultMutableTreeNode root = new DefaultMutableTreeNode(); for ( FTPFile[]
* path : paths) { DefaultMutableTreeNode curr = root; for ( FTPFile value :
* path){ DefaultMutableTreeNode next = null; Enumeration ce = curr.children();
* while (ce.hasMoreElements()){ DefaultMutableTreeNode kid =
* (DefaultMutableTreeNode) ce.nextElement(); if
* (((String)kid.getUserObject()).equals(value)){ next = kid; break; } } if
* (next == null){ next = new DefaultMutableTreeNode(value); curr.add(next); }
* curr = next; } } return root; }
*/
}
我看到您在 Form1 构造函数和 popTree 方法中创建 JTree。我并不清楚 popTree 是否是 Form1 的实例方法。它们都是构造函数和方法范围内的局部变量。
如果其他实例方法需要访问它们,我通常使用字段。因此,不要将它们分配给局部变量,而是将它们分配给字段。这样,其他实例方法就可以轻松访问它们。