1

我正在尝试创建我的第一个JTree,但是当我尝试将节点添加到树时,更改没有生效,并且显示的只是一棵树,其中包含一些默认节点(颜色、运动、食物)。

树所在的主面板代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Scenario1Panel extends JPanel
{

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Scenario1Tree scenario1Tree;

public Scenario1Panel()
{
    scenario1Tree = new Scenario1Tree();
    add(scenario1Tree);
}
public static void main(String args[]) 
{
    try
    {
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        System.out.println(lookAndFeel);
        UIManager.setLookAndFeel(lookAndFeel);
    }
    catch(Exception e)
    {
        System.out.println("couldn't get that LookAndFeel");
    }

    Scenario1Panel mgr = new Scenario1Panel();

    JFrame frame = new JFrame();

    frame.add(mgr);
    frame.pack();
    frame.setVisible(true);

}

}

我的树代码是:

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class Scenario1Tree extends JTree
{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private JTree tree;

public Scenario1Tree()
{
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario1");
    createNodes(top);
    tree = new JTree(top);

    //Create a tree that allows one selection at a time.
    tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);


}

private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode loginUser1 = null;
    DefaultMutableTreeNode addNewUsers = null;
    DefaultMutableTreeNode addNewUsersBreakout = null;

    loginUser1 = new DefaultMutableTreeNode("Login User 1");
    top.add(logintUser1);

    addNewUsers = new DefaultMutableTreeNode("Add New Users");
    top.add(addNewUsers);

    addNewUsersBreakout = new DefaultMutableTreeNode("Add User2");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User3");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User4");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User5");
    addNewUsers.add(addNewUsersBreakout );

}

public static void main(String args[]) 
{
    try
    {
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        System.out.println(lookAndFeel);
        UIManager.setLookAndFeel(lookAndFeel);
    }
    catch(Exception e)
    {
        System.out.println("couldn't get that LookAndFeel");
    }

    Scenario1Tree tree = new Scenario1Tree();

    JFrame frame = new JFrame();

    frame.add(tree);
    frame.pack();
    frame.setVisible(true);

}

}

有人可以解释我做错了什么吗?

4

2 回答 2

6

您创建的 JTree 太多了。您正在将所有节点添加到 JTree 变量中,但对显示的树使用 JTree 扩展类,后者没有添加任何节点。

我建议你不要让你的类扩展 JTree,而是给它一个方法,比如getTree()在需要的时间和地点返回 JTree。

IE,

// get rid of extends JTree
public class Scenario1Tree // extends JTree
{
private static final long serialVersionUID = 1L;

private JTree tree;

public Scenario1Tree()
{
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario1");
    createNodes(top);
    tree = new JTree(top);

    tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);

    // JScrollPane treeView = new JScrollPane(tree);

}

public JTree getTree() {
  return tree;
}
于 2013-01-09T18:05:52.487 回答
5

Scenario1Tree而不是JTree实例创建调用中

setModel(new DefaultTreeModel(top));

事实上,您根本不需要该JTree实例。

于 2013-01-09T18:05:41.963 回答