使用 JTree 获取 Java 中的每个子 类 和同级 类 名称

Using JTree take each sub classes and sibling classes name in Java

我创建了 JavaTree。现在我想获取每个 class 的所有子 classes 和同级 classes 名称。我可以使用 selectedNode.getParent().toString() 获得超级 class。但是下面的子 class 代码给出了像 java.util.Vector@3ae19358 和兄弟 class 这样的结果,我找不到办法。有没有办法给每个子和兄弟姐妹取 classes 的名字?

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;

    public class TreeExample extends JFrame
    {

        /****************** Developing a Simple JTree************************/
      private JTree tree;
      private JLabel selectedLabel;  //for Event Handlers

      public TreeExample()
      {
          //create the root node
          DefaultMutableTreeNode root = new DefaultMutableTreeNode("AA");
          //create the child nodes
          DefaultMutableTreeNode bb = new DefaultMutableTreeNode("BB");
          DefaultMutableTreeNode cc = new DefaultMutableTreeNode("CC");

          //add the child nodes to the root node
          root.add(bb);
          root.add(cc);

          //create the tree by passing in the root node
          tree = new JTree(root);
          add(tree);

          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("JTree Example");       
          this.pack();
          this.setVisible(true);


    /****************** Adding More Children************************/    
          videoInfoNode.add(new DefaultMutableTreeNode("DD"));

          foodInfoNode.add(new DefaultMutableTreeNode("EE"));
          foodInfoNode.add(new DefaultMutableTreeNode("FF"));
          foodInfoNode.add(new DefaultMutableTreeNode("GG"));

    //To get the selected node information       
          tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
              @Override
              public void valueChanged(TreeSelectionEvent e) {
                  DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                  //sub class
                  if(selectedNode.getAllowsChildren()){----??????????
                  selectedLabel.setText(selectedNode.children().toString());}-----?????????
                  else{
                    System.out.println("no children");
                  }

                  //sibling class
                System.out.println(selectedNode.getSibling().....);--------???????? 
               }
          });

    } //end

      //main
      public static void main(String[] args)
      {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new TreeExample();
              }
          });  
      }  
    } 

I could take super class using selectedNode.getParent().toString().

我猜你的意思是 TreeNode

if(selectedNode.getAllowsChildren()){----??????????

也许您需要使用 TreeNode#isLeaf() instead of DefaultMutableTreeNode#getAllowsChildren():

import java.awt.BorderLayout;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class TreeExample2 extends JFrame {
  /****************** Developing a Simple JTree************************/
  private JTree tree;
  //private JLabel selectedLabel;  //for Event Handlers

  public TreeExample2() {
    //create the root node
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("AA");
    //create the child nodes
    DefaultMutableTreeNode bb = new DefaultMutableTreeNode("BB");
    DefaultMutableTreeNode cc = new DefaultMutableTreeNode("CC");

    //add the child nodes to the root node
    root.add(bb);
    root.add(cc);

    //create the tree by passing in the root node
    tree = new JTree(root);
    add(tree);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("JTree Example");
    this.pack();
    this.setVisible(true);


    /****************** Adding More Children************************/
    bb.add(new DefaultMutableTreeNode("DD"));

    cc.add(new DefaultMutableTreeNode("EE"));
    cc.add(new DefaultMutableTreeNode("FF"));
    cc.add(new DefaultMutableTreeNode("GG"));

    //To get the selected node information
    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
      @Override public void valueChanged(TreeSelectionEvent e) {
        Object o = tree.getLastSelectedPathComponent();
        if (o instanceof DefaultMutableTreeNode) {
          //TreeNode selectedNode = (TreeNode) o;
          DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) o;
          System.out.println("----");

          //children
          if (!selectedNode.isLeaf() && selectedNode.getChildCount() > 0) {
            Enumeration enumeration = selectedNode.children();
            while (enumeration.hasMoreElements()) {
              TreeNode node = (TreeNode) enumeration.nextElement();
              System.out.println("child: " + node);
            }
          } else {
            System.out.println("no children");
          }

          //sibling
          TreeNode parentNode = selectedNode.getParent();
          if (parentNode != null) {
            for (int i = 0; i < parentNode.getChildCount(); i++) {
              TreeNode node = parentNode.getChildAt(i);
              if (!selectedNode.equals(node)) {
                System.out.println("sibling: " + node);
              }
            }
          }
        }
      }
    });
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override public void run() {
        new TreeExample2();
      }
    });
  }
}