java动态树形菜单代码 java动态树层级展示-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

java动态树形菜单代码 java动态树层级展示

如何用Java实现树形结构啊?

package tree;

站在用户的角度思考问题,与客户深入沟通,找到城中网站设计与城中网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名网站空间、企业邮箱。业务覆盖城中地区。

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历

*

* 参考资料0:数据结构(C语言版)严蔚敏

*

* 参考资料1:

*

* 参考资料2:

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

public class BinTreeTraverse2 {

private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

private static ListNode nodeList = null;

/**

* 内部类:节点

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

private static class Node {

Node leftChild;

Node rightChild;

int data;

Node(int newData) {

leftChild = null;

rightChild = null;

data = newData;

}

}

public void createBinTree() {

nodeList = new LinkedListNode();

// 将一个数组的值依次转换为Node节点

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树

for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1);

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2);

}

// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理

int lastParentIndex = array.length / 2 - 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果数组的长度为奇数才建立右孩子

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return;

System.out.print(node.data + " ");

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}

/**

* 中序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return;

inOrderTraverse(node.leftChild);

System.out.print(node.data + " ");

inOrderTraverse(node.rightChild);

}

/**

* 后序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void postOrderTraverse(Node node) {

if (node == null)

return;

postOrderTraverse(node.leftChild);

postOrderTraverse(node.rightChild);

System.out.print(node.data + " ");

}

public static void main(String[] args) {

BinTreeTraverse2 binTree = new BinTreeTraverse2();

binTree.createBinTree();

// nodeList中第0个索引处的值即为根节点

Node root = nodeList.get(0);

System.out.println("先序遍历:");

preOrderTraverse(root);

System.out.println();

System.out.println("中序遍历:");

inOrderTraverse(root);

System.out.println();

System.out.println("后序遍历:");

postOrderTraverse(root);

}

}

JSP动态树形菜单,菜单项从数据库中获得

jsp动态树形菜单须用到递归算法,比如在数据库有张表,parent表,parent的字段有id,name,depth,leve,ID自增,depth设置为级数,如这条数据最大,为0,如为字菜单就为1,而leve就指定它父节点的id,给段代码自己可以摸索下 public Vector getModuleTree()

{

Vector pclass = new Vector();

try

{

stmt =con.createStatement();

String sql = "select * from Module where parentid = 0";

rs = stmt.executeQuery(sql);

Module cvo = null;

while(rs.next())

{

cvo = new Module();

cvo.setModule_id(rs.getInt("Module_id"));

cvo.setModule_name(rs.getString("Module_name"));

cvo.setModule_url(rs.getString("Module_url"));

cvo.setParentid(rs.getInt("parentid")); cvo.setRootid(rs.getInt("rootid")); cvo.setDepth(rs.getInt("depth")); pclass.add(cvo);

}

for (int i = 0; i pclass.size(); i++)

{

Module pcvo = (Module) pclass.get(i);

ShowTreeMenu(pcvo);

}

con.commit(); } catch (SQLException e)

{

e.printStackTrace();

} finally

{

try

{

if(rs!=null)

{

rs.close();

}

if(stmt!=null)

{

stmt.close();

}

if(con!=null)

{

con.close();

}

}

catch (SQLException e)

{

e.printStackTrace();

}

}

return classList;

}

public void ShowTreeMenu(Module c)

{

Module ccvo = null;

String sql = "select * from Module where parentid = " + c.getModule_id();

Vector cclass = new Vector();

try

{

Module cvotemp;

stmt =con.createStatement();

rs = stmt.executeQuery(sql);

while(rs.next())

{

cvotemp = new Module();

cvotemp.setModule_id(rs.getInt("Module_id"));

cvotemp.setModule_name(rs.getString("Module_name"));

cvotemp.setModule_url(rs.getString("Module_url"));

cvotemp.setParentid(rs.getInt("parentid")); cvotemp.setRootid(rs.getInt("rootid")); cvotemp.setDepth(rs.getInt("depth")); cclass.add(cvotemp);

}

System.out.println(cclass.size()+"(((((((((((((((((((((((((9");

if (cclass.size() 0)

{

c.setHasChild("have");

classList.add(c);

for (int j = 0; j cclass.size(); j++)

{

ccvo = (Module) cclass.get(j);

ShowTreeMenu(ccvo);

} } else

{

classList.add(c);

}

} catch (SQLException e)

{

e.printStackTrace();

}

}

java如何实现二级树形菜单动态显示。要求加载页面时显示一级,点击一级菜单再去数据库查找出二级菜单

用json格式,异步请求,当你点击一级菜单的时候,js触发,通过ajax异步把相关参数传到后台,查询出来的数据,然后封装成json格式数据返回,js拿到数据后,通过js把数据设置到页面相应的地方!

java带有checkbox的树型菜单

div

style="width: 100%; height: 94%; float: left; background:#f7f7f1;overflow-y:auto;"

id="treeDiv"/div

script type="text/javascript"

var selectedNodeID = '';

var org_treeList = new dhtmlXTreeObject(document.getElementById('treeDiv'),"100%","100%",0);

org_treeList.setImagePath("%=path%/images/tree/");

org_treeList.enableIEImageFix(true);

org_treeList.enableCheckBoxes(false);

org_treeList.enableDragAndDrop(false);

org_treeList.enableCheckBoxes(1);

org_treeList.enableThreeStateCheckboxes(true);

org_treeList.attachEvent("onOpenStart", function (id, state) {

if(state == '0')

{

org_treeList.setItemImage(id, 'folderOpen.gif','loading.gif');

}

if(state =='1')

{

org_treeList.setItemImage(id, 'folderOpen.gif','folderClosed.gif');

}

return true;

});

org_treeList.setXMLAutoLoading("你的xml地址");

org_treeList.loadXML("你的XML地址");

/script

上面是页面中的代码 这样的写法是动态加载的, 你可以去下面的地址查看具体用法


本文题目:java动态树形菜单代码 java动态树层级展示
文章URL:http://kswsj.cn/article/ddcicoe.html

其他资讯