0
DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
c.add(vinod);
vinod.add(swing);
swing.add(tr);
myComputer.add(c);
myComputer.add(a);
myComputer.add(e);


JTree tree = new JTree(myComputer);
JScrollPane scrollPane = new JScrollPane(tree);
jPanel1.add(scrollPane);
tree.setVisible(true);

我从网上获得了新的树示例,但是当我尝试显示它时,它没有出现!我不知道为什么。有任何想法吗?谢谢!

4

1 回答 1

2

将代码调整为SSCCE,在这里可以正常工作

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();

        DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
        DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
        DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
        DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
        DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
        DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
        DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
        c.add(vinod);
        vinod.add(swing);
        swing.add(tr);
        myComputer.add(c);
        myComputer.add(a);
        myComputer.add(e);


        JTree tree = new JTree(myComputer);
        JScrollPane scrollPane = new JScrollPane(tree);
        frame.getContentPane().add(scrollPane);
        tree.setVisible(true);

        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
      }
    } );
  }
于 2013-08-24T07:36:46.613 回答