0

JTree関連する変数からノードを体系的に追加する単純な があります。

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);
}

私が望むのは、余分なselection.insertsを避けるために、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] );
}

どうすればいいですか?

ありがとう

4

2 に答える 2

0

解決策は非常に簡単です。

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

セクション[i]には二重括弧が必要です

于 2013-07-20T14:31:55.007 に答える