[CODE]package TreeFrame;
import javax.swing.*;
import javax.swing.tree.*;
class Tree extends JTree {
/**
* @param args
*/
static DefaultMutableTreeNode root = new
DefaultMutableTreeNode("资源管理器");
static DefaultMutableTreeNode root1 = new
DefaultMutableTreeNode("桌面");
static DefaultTreeModel treeModel = new DefaultTreeModel(root);
static{
DefaultMutableTreeNode node1 = new
DefaultMutableTreeNode("我的电脑");
DefaultMutableTreeNode node2 = new
DefaultMutableTreeNode("回收站");
treeModel.insertNodeInto(node1, root1, root1.getChildCount());
treeModel.insertNodeInto(node2, root1, root1.getChildCount());
}
JTree tree = new JTree(treeModel);
JScrollPane scrollPane = new JScrollPane();
public Tree() {
treeModel.insertNodeInto(root1, root, 0);
setTree();
//new TreeMenu();
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception el) {
el.printStackTrace();
System.exit(0);
}
}
public void setTree() {
scrollPane.setViewportView(tree);
}
public JScrollPane getTree() {
return scrollPane;
}
}[/CODE]
这样就不会了,但是类最好不要这个设计
你知道开始为什么会加两个吗?因为你的构造函数调用了两次,你把加进去的操作放在构造函数里面,当然会加两篇了
现在把它弄成静态初始化块,就只会执行一次了
Java,My Love!