小编只是展示一下JTree的简单使用。
在这里插入代码片
import java.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Main1 extends JApplet{
private JTree tree;
public Main1() {
tree=new JTree();
add(tree);
JButton button=new JButton("显示");
add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
TreePath path=tree.getSelectionPath();//返回被选择路径
System.out.println(path.getLastPathComponent());//返回路径最后一个分支节点
System.out.println(path.getPathCount());//返回备选路径中节点的个数
TreeNode a=(TreeNode) path.getLastPathComponent();
while(a!=null) {//打印被选路径中的结点
System.out.print(a.toString()+" ");
a=a.getParent();
}
}});
}
}