|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
C#是盗用了Java的源代码,仿照开发的,原因是Java是开源的啊,盗了也白盗,还有一点,开发C#语言的团队是就是开发Java语言的团队,是微软重金挖过去的啊程序该程序能够依据从指定的xml文件中读取的有关信息静态的创建一棵树。
1.该树的特性:
1)无穷级树布局
实际上树的深度能够无穷,树的深度由xml文件所给的信息断定;
数据加载接纳一次加载和静态加载相分离的办法;
每次翻开相干页时从头加载树布局,真实的做到了创建树的静态性。
2)复用性
因为这棵树是经由过程读取xml文件创建的,树的程序代码流动,以是具有很强的复用性。只必要给出xml文件,程序就能够创建对应的树。
3)与用户的交互
因为该程序是用java小程序完成的,以是能够使用java言语中监听事务的类,以呼应用户操纵。与用户具有更好的交互性。
2.程序布局
这棵树次要有三个类完成:
1)类TreeNode:该类起帮助感化,用来形貌树的数据布局,并供应一些setter,getter函数用来设置树布局的属性。
2)类IconRender:该类也是一个帮助类,用来设置树节点的图标。而且能够依据树节点的选中形态改动图标。
3)类AppletTree:该类是创建这棵树的次要类,它承继自Applet而且完成了TreeSelectionListener接口,以用来监听用户操纵。树布局由java类JTree显现,我们只必要公道的设定JTree的属性就能够机关出这棵树。而它的属性是经由过程jdom中的Element读取xml文件供应的。下面已提到监听用户操纵是经由过程完成TreeSelectionListener接口完成的,可是只监听不呼应毫无用途。如何才干将监听到的用户操纵传给响应的类以呼应用户的操纵呢?这里用的办法是经由过程URL将参数传送给jsp文件,再由jsp文件传给响应的操纵类完成的。
这三个类的代码以下:
1.TreeNode类
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;
}
}
2.IconRender类
importjavax.swing.*;
importjava.awt.*;
importjavax.swing.tree.*;
importjavax.swing.tree.DefaultTreeCellRenderer;
IconRender
classextendsDefaultTreeCellRenderer{
//创立用于显现的图标
publicstaticfinalImageIconleafSelectedIcon=newImageIcon("OpenedFolder.ico");
publicstaticfinalImageIconleafUnSelectedIcon=newImageIcon("closedFolder.ico");
publicstaticfinalImageIconfolderOpen=newImageIcon("OpenedFolder.ico");
publicstaticfinalImageIconfolderClose=newImageIcon("closedFolder.ico");
//设定图标随选中形态的变更
publicComponentgetTreeCellRendererComponent(JTree
tree,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();
if((leafSelectedIcon!=null)&&(leafUnSelectedIcon!=null))
System.out.println("IconRender:trace");
setLeafIcon(leafUnSelectedIcon);
setOpenIcon(folderOpen);
setClosedIcon(folderClose);
}
}
3.AppletTree类
importjavax.swing.event.*;
importjava.awt.*;
importjava.applet.*;
importjavax.swing.*;
importjavax.swing.tree.*;
importjava.awt.event.*;
importorg.jdom.*;
//importorg.w3c.dom.NodeList;
importorg.jdom.input.*;
importjava.io.*;
importjava.util.*;
importjava.net.*;
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.border.*;
importjavax.swing.plaf.*;
importjavax.swing.plaf.basic.*;
importjavax.swing.plaf.metal.*;
importjava.io.*;
//importnetscape.javascript.*;
classMyWindowListenerextendsWindowAdapter
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(1);
}
}
publicclassAppletTreeextendsAppletimplementsTreeSelectionListener
{
privateJTreetree;
privateTreePathpath;
privateJScrollPanetreeView;
privateDefaultMutableTreeNodetop;
privateDefaultMutableTreeNodeclicknode;
privateStringlink;
publicAppletTree(){
}
MouseListenerml=newMouseAdapter(){
publicvoidmousePressed(MouseEvente){
intselRow=tree.getRowForLocation(e.getX(),e.getY());
TreePathselPath=tree.getPathForLocation(e.getX(),e.getY());
if(selRow!=-1){
if(e.getClickCount()==1){
System.out.print("点击一次鼠标");
//失掉最初一个节点
DefaultMutableTreeNodetreeNode=(DefaultMutableTreeNode)selPath.getLastPathComponent();
booleanisLeaf=treeNode.isLeaf();
TreeNodeobjTreeNode=(TreeNode)treeNode.getUserObject();
System.out.println(isLeaf);
System.out.println(objTreeNode.getName());
System.out.println(objTreeNode.getId());
if(isLeaf)
{//假如为叶子节点就传进来举行查询操纵
try
{
getAppletContext().showDocument(newURL("http://127.0.0.1:8080/tree/showDevice.htm"),"f2");
}
catch(Exceptionex)
{
System.out.print(ex);
}
}
}
elseif(e.getClickCount()==2){
System.out.print("点击两次鼠标");
//getAppletContext().showDocument(newURL("http://www.csdn.net/"));
//myDoubleClick(selRow,selPath);
}
}
}
};
publicvoidstart()
{
super.start();
}
publicvoidinit(){
try{
super.init();
this.setLayout(newGridLayout(1,1));
tree=createTree(newFileInputStream("e:/example/TreeXML.xml"));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.putClientProperty("JTree.lineStyle","Angled");
tree.setShowsRootHandles(true);
tree.setEditable(true);
tree.addTreeSelectionListener(this);
IconRenderrender=newIconRender();
tree.setCellRenderer(render);
treeView=newJScrollPane(tree);
//topPanel.add(tree);
this.add(treeView);
tree.addMouseListener(ml);
}catch(Exceptione){
e.printStackTrace();
}
}
publicJTreecreateTree(InputStreamis){
SAXBuilderbuilder=newSAXBuilder();
try{
Documentdoc=builder.build(is);
Elementroot=doc.getRootElement();
TreeNoderootNode=new
TreeNode(root.getAttributeValue("id"),root.getAttributeValue("name"),root.getAttributeValue("HaveChildren"));
top=newDefaultMutableTreeNode(rootNode);
addNode(root,top);
}catch(Exceptionex){
ex.printStackTrace();
}
//能够在这里改动jtree中连线的色彩
UIManager.put("Tree.hash",newColorUIResource(Color.red));
returnnewJTree(top);
}
/**
*
*@parame待到场树中的jdom元素
*@paramfatherNode树节点父亲节点
*/
privatevoidaddNode(Elemente,DefaultMutableTreeNodefatherNode){
try{
Iteratorit=e.getChildren().iterator();
if(!it.hasNext())
return;
while(it.hasNext()){
Elementsub=(Element)it.next();
Stringid=newString(sub.getAttributeValue("id").getBytes(),"GB2312");
Stringname=newString(sub.getAttributeValue("name").getBytes(),"GB2312");
Stringlink=newString(sub.getAttributeValue("HaveChildren").getBytes(),"GB2312");
System.out.println(id);
System.out.println(name);
System.out.println(link);
TreeNodetempNode=newTreeNode(id,name,link);
DefaultMutableTreeNodenode=newDefaultMutableTreeNode(tempNode);
fatherNode.add(node);
if(link.equals("true")){
System.out.println("true~!equaltest");
addNode(sub,node);
}
}
}
catch(Exceptionexp)
{
System.out.println("encodingfail");
}
}
/**
*依据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();
}
}
}
publicstaticvoidmain(String[]args){
JFrameframe1=newJFrame("test");
AppletTreetree=newAppletTree();
tree.init();
frame1.getContentPane().add(tree);
frame1.setSize(600,600);
frame1.show();
frame1.addWindowListener(newMyWindowListener());
}
}
还有就是总有人问我到底该学习什么语言,什么语言有前途,那么我的回答是不论是C,C++,java,.net,ruby,asp或是其他语言都可以学,编程的关键不是语言,而是思想。 |
|