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);
我从网上获得了新的树例子,但是当我尝试显示它时,它没有出现!我不知道为什么。有任何想法吗? 谢谢!
答案 0 :(得分: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 );
}
} );
}