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);
JTree tree = new JTree(treeModel);
JScrollPane scrollPane = new JScrollPane();
public Tree() {
treeModel.insertNodeInto(root1, root, 0);
setTree();
DefaultMutableTreeNode node1 = new
DefaultMutableTreeNode("我的电脑");
DefaultMutableTreeNode node2 = new
DefaultMutableTreeNode("回收站");
treeModel.insertNodeInto(node1, root1, root1.getChildCount());
treeModel.insertNodeInto(node2, root1, root1.getChildCount());
//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;
}
}
上面红色的地方,如果变量声明为static的 这时就会添加两次,不明白为什么。。。
还有一个问题就是,我不把这里的变量声明为static的话,同一个包中的其它类为什么不能引用!!