如何用 Java 获取 JTree 中节点的叶节点?


若要获取节点的叶子,请使用 getLeafCount() 方法。

假设你想获取整个树的叶子,那么请使用根节点,假设 “node” 是我们的根节点 −

node.getLeafCount()

现在,假设我们想获取不是根节点的节点的叶子,因此设置 node。此处,node1 不是根节点 −

node1.getLeafCount()

以下是一个获取节点叶子的示例 −

示例

package my;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Demo");
      DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");
      DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");
      DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Tutorials");
      DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("QA");
      DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Tools");
      node.add(node1);
      node.add(node2);
      node.add(node3);
      node.add(node4);
      DefaultMutableTreeNode one = new DefaultMutableTreeNode("PHP Videos");
      DefaultMutableTreeNode two = new DefaultMutableTreeNode("HTML5 Videos");
      DefaultMutableTreeNode three = new DefaultMutableTreeNode("Blockchain Videos");
      DefaultMutableTreeNode four = new DefaultMutableTreeNode("Java");
      DefaultMutableTreeNode five = new DefaultMutableTreeNode("DBMS");
      DefaultMutableTreeNode six = new DefaultMutableTreeNode("CSS");
      DefaultMutableTreeNode seven = new DefaultMutableTreeNode("MongoDB");
      DefaultMutableTreeNode eight = new DefaultMutableTreeNode("Python QA");
      DefaultMutableTreeNode nine = new DefaultMutableTreeNode("jQuery QA");
      DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Photo Editing Tool");
      node1.add(one);
      node1.add(two);
      node1.add(three);
      node2.add(four);
      node2.add(five);
      node2.add(six);
      node2.add(seven);
      node3.add(eight);
      node3.add(nine);
      node4.add(ten);
      JTree tree = new JTree(node);
      for (int i = 0; i < tree.getRowCount(); i++) {
         tree.expandRow(i);
      }
      tree.putClientProperty("JTree.lineStyle", "Angled");
      System.out.println("Number of children of node1 (Videos) = " + node1.getChildCount());
      System.out.println("Number of children of node2 (Tutorials) = " + node2.getChildCount());
      System.out.println("Number of children of node3 (QA) = " + node3.getChildCount());
      System.out.println("Number of children of node4 (Tools) = " + node4.getChildCount());
      System.out.println("Depth of Tree = " + node.getDepth());
      System.out.println("Count of Tree Leaves(root node) = " + node.getLeafCount());
      System.out.println("Count of Tree Leaves(node1) = " + node1.getLeafCount());
      System.out.println("Count of Tree Leaves(node2) = " + node2.getLeafCount());
      System.out.println("Count of Tree Leaves(node3) = " + node3.getLeafCount());
      System.out.println("Count of Tree Leaves(node3) = " + node4.getLeafCount());
      tree.setRowHeight(25);
      frame.add(tree);
      frame.setSize(600,450);
      frame.setVisible(true);
   }
}

输出

以下是我们的 JTree −

更新时间: 30-7 月-2019

403 次浏览

启动您的 职业

通过完成课程获得认证

开始
广告