簡體   English   中英

JTree-如何使用前循環添加節點?

[英]JTree - how to add nodes with a For-Loop?

我有一個簡單的JTree ,可以系統地從相關var中添加節點:

public void init()
{   
    final String section1 = "JAVA";

    final String section1_content1 = "Tutorial1";
    final String section1_content2 = "Tutorial2";
    final String section1_content3 = "Tutorial3";
    final String section1_content4 = "Tutorial4";
    final String section1_content5 = "Tutorial5";
    final String section1_content6 = "Tutorial6";

    final String content1a = "Introduction";
    final String content1b = "Hello World!";

    // Create the title node:
    title = new DefaultMutableTreeNode(section1);

    // Create and attach the 1st subtree:
    selection = new DefaultMutableTreeNode(section1_content1);

    selection.insert(new DefaultMutableTreeNode(content1a),0);
    selection.insert(new DefaultMutableTreeNode(content1b),0);

    title.insert(selection,0);
}

我想要的是使用For-Loop,以避免額外的選擇。

就像是

String[] sections = new String[]{ "Tutorial1", "Tutorial2", "Tutorial3", "Tutorial4", "Tutorial5", "Tutorial6" };

for (int i=0; i < sections.length; i++) { 
    selection = new DefaultMutableTreeNode( sections[i] );
}

我該怎么做?

謝謝

您可以將所有值放入一個枚舉,然后遍歷該枚舉。

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

解決方案非常簡單:

    for (int i=0; i<sections.length; i++) {
    selection = new DefaultMutableTreeNode(( sections[i]));
    title.insert(selection,0);
    }

第[i]節需要使用雙括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM