|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
而学习JAVA我觉得最应该避免的就是:只学习,不思考,只记忆,不实践!dom|xml|数据
转自:http://www.softhouse.com.cn/html/200410/2004102517145700001335.html
利用jdom操纵xml数据,天生含Jtree的applet
在我们事情中,经常会碰着树形组件的天生成绩,假如你在开辟webapplication,地道利用javascript来天生树形组件长短常烦琐的,并且交互性也不不太好。以是很多产物使applet来完成树形组件的功效。好比说,weblogic,jboss等产物的console.以是,把树形数据构造成xml文件,用jdom分析它,最初天生applet就十分有通用的意义。上面,我就给出一个例子,举一反三。1.筹办一个存有属性数据的xml文件,把它放在classpath中,我这里是org.xml。[pre]<?xmlversion="1.0"encoding="UTF-8"?><!--SampleXMLfilegeneratedbyXMLSPYv5rel.3U(http://www.xmlspy.com)--><nodexmlns="http://www.javabox.com/schemas/org"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.javabox.com/schemas/orgE:myDemoorg.xsd"name="构造机构"id="-1"desc=""link="#"><nodename="总司理"id="1"desc=""link="#"><nodename="办理副总司理"id="2"desc=""link="#"/><nodename="临盆副总司理"id="3"desc=""link="#"><nodename="项目部"id="7"desc=""link="#"/><nodename="机器公司"id="8"desc=""link="#"/><nodename="贝盟公司"id="9"desc=""link="#"/><nodename="洛斯韦公司"id="9"desc=""link="#"/></node><nodename="总工程师"id="4"desc=""link="#"/><nodename="总管帐师"id="5"desc=""link="#"/><nodename="总经济师"id="6"desc=""link="#"/></node></node>[/pre]2.确保你可使用jdom剖析器,你假如没有能够往这里下载。3.用于代表树结点节点的javabean,TreeNode.javapackagecom.javabox.jtree;publicclassTreeNode{privateStringid;privateStringname;privateStringlink;publicTreeNode(Stringid,Stringname,Stringlink){this.id=id;this.name=name;this.link=link;}publicStringgetId(){returnid;}publicvoidsetId(StringId){this.id=Id;}publicvoidsetName(StringName){this.name=Name;}publicStringgetName(){returnname;}publicStringtoString(){returnname;}publicStringgetLink(){returnlink;}publicvoidsetLink(Stringlink){this.link=link;}}4.本人写的TreeCellRenderer,IconRender.javapackagecom.javabox.jtree;importjavax.swing.*;importjava.awt.*;importjavax.swing.tree.*;importjavax.swing.tree.DefaultTreeCellRenderer;classIconRenderextendsDefaultTreeCellRenderer{//你必要交换成你的iconpublicstaticfinalIconleafSelectedIcon=newImageIcon("greeball.JPG");publicstaticfinalIconleafUnSelectedIcon=newImageIcon("greyball.JPG");publicstaticfinalIconfolderOpen=newImageIcon("folderopen.JPG");publicstaticfinalIconfolderClose=newImageIcon("folderclose.JPG");publicComponentgetTreeCellRendererComponent(JTreetree,Objectvalue,booleanselected,booleanexpanded,booleanleaf,introw,booleanhasFocus){super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);if(leaf&&selected){setIcon(IconRender.leafSelectedIcon);}elseif(leaf){setIcon(IconRender.leafUnSelectedIcon);}returnthis;}publicIconRender(){super();this.setLeafIcon(leafUnSelectedIcon);this.setOpenIcon(folderOpen);this.setClosedIcon(folderClose);}}5.AppletTree.java,该文件剖析xml文件,天生含Jtree的applet,你能够把它嵌进到jsp,html文件中利用,也能够间接运转该文件。packagecom.javabox.jtree;importjavax.swing.event.*;importjava.awt.*;importjava.applet.*;importjavax.swing.*;importjavax.swing.tree.*;importjava.awt.event.*;importorg.jdom.*;importorg.jdom.input.*;importjava.io.*;importjava.util.*;importjava.awt.*;importjavax.swing.*;importjavax.swing.border.*;importjavax.swing.plaf.*;importjavax.swing.plaf.basic.*;importjavax.swing.plaf.metal.*;importjava.io.*;importnetscape.javascript.*;publicclassAppletTreeextendsAppletimplementsTreeSelectionListener{privateJTreetree;privateTreePathpath;privatePaneltopPanel;privateDefaultMutableTreeNodetop;privateDefaultMutableTreeNodeclicknode;privateStringlink;publicAppletTree(){}publicvoidinit(){try{super.init();this.setLayout(newGridLayout(1,1));tree=createTree(newFileInputStream("org.xml"));tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);tree.putClientProperty("JTree.lineStyle","Angled");tree.setShowsRootHandles(true);tree.setEditable(false);tree.addTreeSelectionListener(this);IconRenderrender=newIconRender();tree.setCellRenderer(render);topPanel=newPanel(newBorderLayout());topPanel.add(tree);this.add(topPanel);}catch(Exceptione){e.printStackTrace();}}publicJTreecreateTree(InputStreamis){SAXBuilderbuilder=newSAXBuilder();try{Documentdoc=builder.build(is);Elementroot=doc.getRootElement();TreeNoderootNode=newTreeNode(root.getAttributeValue("id"),root.getAttributeValue("name"),root.getAttributeValue("link"));top=newDefaultMutableTreeNode(rootNode);addNode(root,top);}catch(Exceptionex){ex.printStackTrace();}//你能够在这里改动jtree中连线的色彩,我就教外洋的妙手才找到的,很酷的哦:)UIManager.put("Tree.hash",newColorUIResource(Color.red));returnnewJTree(top);}/****@parame待到场树种的jdom元素*@paramrootNode树根节点*/privatevoidaddNode(Elemente,DefaultMutableTreeNoderootNode){Stringid=e.getAttributeValue("id");Stringname=e.getAttributeValue("name");Stringlink=e.getAttributeValue("link");TreeNodenode=newTreeNode(id,name,link);//若有父节点Elementfather=e.getParent();if(father!=null){Stringfid=father.getAttributeValue("id");DefaultMutableTreeNodefatherNode=getTreeNode(fid,rootNode);if(fatherNode!=null){fatherNode.add(newDefaultMutableTreeNode(node));}}//若有子节点Iteratorit=e.getChildren().iterator();while(it.hasNext()){Elementchild=(Element)it.next();addNode(child,rootNode);}}/***依据id,查找树节点,//广度优先*@paramid节点id*@paramrootNode树根节点*@returnDefaultMutableTreeNode*/privateDefaultMutableTreeNodegetTreeNode(Stringid,DefaultMutableTreeNoderootNode){DefaultMutableTreeNodereturnNode=null;if(rootNode!=null){Enumerationenum=rootNode.breadthFirstEnumeration();while(enum.hasMoreElements()){DefaultMutableTreeNodetemp=(DefaultMutableTreeNode)enum.nextElement();TreeNodenode=(TreeNode)temp.getUserObject();if(node.getId().equals(id)){returnNode=temp;break;}}}returnreturnNode;}publicvoidvalueChanged(TreeSelectionEventevent){if(event.getSource()==tree){path=event.getPath();clicknode=(DefaultMutableTreeNode)path.getLastPathComponent();Objectuo=clicknode.getUserObject();if(uoinstanceofTreeNode){TreeNodend=(TreeNode)clicknode.getUserObject();link=nd.getLink();}//挪用一个javascript函数; //JSObject.getWindow(this).eval("javascript:window.open("+link+")");}}publicstaticvoidmain(String[]args){JFrameframe=newJFrame("test");AppletTreetree=newAppletTree();tree.init();frame.getContentPane().add(tree);frame.setSize(600,600);frame.show();}}6.运转一个这个类,是否是很cool哦,你还能够把它嵌在网页中,挪用javasript函数,到达革新页面的目标。
诸如RMI,EJB等一些技术并不是你说的那么复杂,而是它们把一些复杂的工具封装成不复杂的工具了,理解这些工具是需要些时间。我问你,.net里有这些工具吗?要简单多少?。 |
|