public class Tree extends JFrame{
JTextField textField;
public Tree(){//构造方法
DefaultMutableTreeNode root=new DefaultMutableTreeNode("根节点");
DefaultMutableTreeNode nodeFirst=new DefaultMutableTreeNode("一级子节点A");
root.add(nodeFirst);
DefaultMutableTreeNode nodeSecond=new DefaultMutableTreeNode("二级子节点a");
nodeFirst.add(nodeSecond);
DefaultMutableTreeNode nodeThree=new DefaultMutableTreeNode("三级子节点");
nodeSecond.add(nodeThree);
DefaultMutableTreeNode nodeFirst2=new DefaultMutableTreeNode("一级子节点B");
root.add(nodeFirst2);
DefaultMutableTreeNode nodeSecond2=new DefaultMutableTreeNode("二级子节点b");
nodeFirst2.add(nodeSecond2);;
JTree treeRoot=new JTree(root);//利用根节点直接创建树
/*treeRoot.setRootVisible(false);//不显示树的根节点
treeRoot.setRowHeight(20);//树节点行高
treeRoot.setFont(new Font("宋体",Font.BOLD,14));//设置树节点字体
treeRoot.putClientProperty("JTree.lineStyle","None");//节点间不采用连线
DefaultTreeCellRenderer treeCellRenderer=(DefaultTreeCellRenderer)treeRoot.getCellRenderer();//获得树节点的绘制对象
treeCellRenderer.setLeafIcon(null);//设置叶子节点不采用图标
treeCellRenderer.setClosedIcon(null);//设置节点折叠是不采用图标
treeCellRenderer.setOpenIcon(null);//设置节点展开是不采用图标
Enumeration enumeration=root.preorderEnumeration();//按前序遍历所有树节点
while(enumeration.hasMoreElements()){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)enumeration.nextElement();
if(!node.isLeaf()){
TreePath path=new TreePath(node.getPath());//创建该节点的路径
treeRoot.expandPath(path);//如果不是叶子节点则展开该节点
}
}*/
DefaultTreeModel treeModel=new DefaultTreeModel(root);//利用根节点创建树模型自己判断
getContentPane().add(treeRoot, BorderLayout.CENTER);
final JButton addButton=new JButton("添加");
textField=new JTextField(20);
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
DefaultMutableTreeNode node=new DefaultMutableTreeNode(textField.getText());//创建想添加节点
TreePath selectionPath=treeRoot.getSelectionPath();//获得选中的父节点路径
DefaultMutableTreeNode parentNode=(DefaultMutableTreeNode)selectionPath.getLastPathComponent();//获得选中的父节点
treeModel.insertNodeInto(node,parentNode,parentNode.getChildCount());//插入节点到所有节点的么节点
TreePath path=selectionPath.pathByAddingChild(node);//获得新添加新节点路径
if(!treeRoot.isVisible(path))
treeRoot.makeVisible(path);//如果不可见零他可见
}
});
JPanel panel=new JPanel();
panel.add(addButton);
final JButton updButton=new JButton("修改");
updButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TreePath selectionPath=treeRoot.getSelectionPath();//获得选中的想修改的节点路径
DefaultMutableTreeNode node=(DefaultMutableTreeNode)selectionPath.getLastPathComponent();//获得选中的想修改的节点
node.setUserObject(textField.getText());//修改阶段的用户
treeModel.nodeChanged(node);//通知树模型该节点已被修改
TreePath path=selectionPath.pathByAddingChild(node);
if(!treeRoot.isVisible(path)){
treeRoot.makeVisible(path);
}
treeRoot.setSelectionPath(selectionPath);//选中被修改的节点
}
});
panel.add(updButton);
final JButton delButton=new JButton("删除");
delButton.addActionListener(new ActionListener(){
public void actionPerformd(ActionEvent e){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treeRoot.getLastSelectedPathComponent();//获得向北删除的节点
//查看想删除的是否为根节点,根节点不允许删除
if(!node.isRoot()){
DefaultMutableTreeNode nextSelectedNode=node.getNextSibling();//获得下一个兄弟节点已被选中
if(nextSelectedNode==null){//查看是否存在
nextSelectedNode=(DefaultMutableTreeNode)node.getParent();
treeModel.removeNodeFromParent(node);//删除节点
TreePath selectionPath=treeRoot.getSelectionPath();//获得选中的父节点路径
treeRoot.setSelectionPath(new TreePath(nextSelectedNode.getPath()));
TreePath path=selectionPath.pathByAddingChild(node);
if(!treeRoot.isVisible(path)){
treeRoot.makeVisible(path);
}
treeRoot.setSelectionPath(selectionPath);//选中被修改的节点
}
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
panel.add(delButton);
textField.setBounds(0,220,50,20);
getContentPane().add(textField,BorderLayout.SOUTH);
getContentPane().add(panel, BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Tree();
}
}
节点增删改
最新推荐文章于 2020-02-03 10:55:36 发布