使用 Java 获取 JTree 中节点上方的级别数


要获取节点上方的级别数,请使用 getLevel() 方法。以下是根节点“node”的示例 -

node.getLevel()

注意 - 如果节点是根节点,则返回 0,因为根节点上方没有级别。

对于其他节点,请获取节点上方的级别数,如下所示,其中 node3 -

node3.getLevel()

以下是在 JTree 中获取节点上方的级别数的示例 -

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("Project");
      DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Domain");
      DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Topics");
      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("Management ");
      DefaultMutableTreeNode two = new DefaultMutableTreeNode("Finance");
      DefaultMutableTreeNode three = new DefaultMutableTreeNode("Networking");
      DefaultMutableTreeNode four = new DefaultMutableTreeNode("MIS");
      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("Code Formatter 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 root node = " + node.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("Number of levels above root node = " + node.getLevel());
      System.out.println("Number of levels above node1 = " + node1.getLevel());
      System.out.println("Number of levels above node2 = " + node2.getLevel());
      System.out.println("Number of levels above node3 = " + node3.getLevel());
      System.out.println("Number of levels above node4 = " + node4.getLevel());
      tree.setRowHeight(20);
      frame.add(tree);
      frame.setSize(600,450);
      frame.setVisible(true);
   }
}

以下是 JTree -

输出

更新于:2019 年 7 月 30 日

276 次查看

启动您的 事业

完成课程即可获得认证

开始
广告