import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.event.*
import javax.swing.tree.*
public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null
JTree tree=null
DefaultTreeModel treeModel=null
String nodeName=null//原有节点名称
public TreeTest(){
JFrame f=new JFrame("TreeTest")
Container contentPane=f.getContentPane()
DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器")
tree=new JTree(root)
tree.setEditable(true)
tree.addMouseListener(new MouseHandle())
treeModel=(DefaultTreeModel)tree.getModel()
treeModel.addTreeModelListener(this)
JScrollPane scrollPane=new JScrollPane()
scrollPane.setViewportView(tree)
JPanel panel=new JPanel()
JButton b=new JButton("新增节点")
b.addActionListener(this)
panel.add(b)
b=new JButton("删除节点")
b.addActionListener(this)
panel.add(b)
b=new JButton("清除所有节点")
b.addActionListener(this)
panel.add(b)
label=new JLabel("Action")
contentPane.add(panel,BorderLayout.NORTH)
contentPane.add(scrollPane,BorderLayout.CENTER)
contentPane.add(label,BorderLayout.SOUTH)
f.pack()
f.setVisible(true)
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0)
}
})
}
//本方法运行新增、删除、清除所有节点的程序代码.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("新增节点")){
DefaultMutableTreeNode parentNode=null
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点")
newNode.setAllowsChildren(true)
TreePath parentPath=tree.getSelectionPath()
//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent())
//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount())
//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()))
label.setText("新增节点成功")
}
if (ae.getActionCommand().equals("删除节点")){
TreePath treepath=tree.getSelectionPath()
if (treepath!=null){
//下面两行取得选取节点的父节点.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent()
TreeNode parent=(TreeNode)selectionNode.getParent()
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModel.removeNodeFromParent(selectionNode)
label.setText("删除节点成功")
}
}
}
if (ae.getActionCommand().equals("清除所有节点")){
//下面一行,由DefaultTreeModel的getRoot()方法取得根节点.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot()
//下面一行删除所有子节点.
rootNode.removeAllChildren()
//删除完后务必运行DefaultTreeModel的reload() *** 作,整个Tree的节点才会真正被删除.
treeModel.reload()
label.setText("清除所有节点成功")
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=e.getTreePath()
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent()
try{
int[] index=e.getChildIndices()
node=(DefaultMutableTreeNode)node.getChildAt(index[0])
}catch(NullPointerException exc){}
label.setText(nodeName+"更改数据为:"+(String)node.getUserObject())
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println("new node insered")
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println("node deleted")
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println("Structrue changed")
}
public static void main(String[] args){
new TreeTest()
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource()
int rowLocation=tree.getRowForLocation(e.getX(),e.getY())
TreePath treepath=tree.getPathForRow(rowLocation)
TreeNode treenode=(TreeNode)treepath.getLastPathComponent()
nodeName=treenode.toString()
}catch(NullPointerException ne){}
}
}
}
/*这里使用了dom4j组件,你需要自己去下载dom4j。
其中a.xml是你的源文件。
这个程序没有向磁盘中创建一个新文件,你可以自己修改代码。
例如:
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format)
可以修改为
XMLWriter xmlWriter = new XMLWriter(new FileWriter("a.xml"), format)
这样就可以修改源文件a.xml了。
*/
import org.dom4j.*
import org.dom4j.io.OutputFormat
import org.dom4j.io.XMLWriter
import org.dom4j.io.SAXReader
import java.io.*
public class Test {
public static void main(String[] args) {
SAXReader saxReader = new SAXReader()
try {
Document doc = saxReader.read(new File("a.xml"))
doc.getRootElement().addElement("PARAM")
.addElement("TASKLIST").addElement("TASK")
.addElement("DATA_TRAN_ID").addCDATA("14595")
OutputFormat format = OutputFormat.createPrettyPrint()
format.setEncoding("gb2312")
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format)
xmlWriter.write(doc)
xmlWriter.close()
} catch (DocumentException e) {
e.printStackTrace()
} catch(IOException e) {
e.printStackTrace()
}
}
}
2L谁说必须用递归的,只是递归写起来简单罢了,迭代的方法一样OK;深度优先或者广度优先都可以
LZ的问题用迭代的方法可以这样解决:
1。把当前节点(需要查找字节点的节点)压入一个堆栈,这步是初始化;
2。从堆栈中d出一个节点,如果该节点是叶子节点,则这条路已经走不通了,如果是非叶子节点,那就把这个节点的所有子节点压入堆栈
3。重复第二步直到堆栈为空
上面三步就能遍历当前节点的所有字节点
递归的话:
f(node){
for(遍历node的所有子节点){
child=当前子节点
if(child为叶子节点){
..return
}
else{
f(child)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)