「java树形菜单算法」java实现菜单树

博主:adminadmin 2022-11-28 02:38:09 60

本篇文章给大家谈谈java树形菜单算法,以及java实现菜单树对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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 生成一个树形菜单

package com.csii;

import java.awt.GridLayout;

import javax.swing.JEditorPane;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTree;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeSelectionModel;

public class TreeDemo1 extends JFrame implements TreeSelectionListener {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JTree jTree = null;

private JEditorPane jEditorPane = null;

private JScrollPane jScrollPane = null;

public JScrollPane getjScrollPane() {

if(jScrollPane==null){

jScrollPane = new JScrollPane();

jScrollPane.setViewportView(getJTree());

}

return jScrollPane;

}

DefaultMutableTreeNode top = null; // @jve:decl-index=0:

/**

 * This is the default constructor

 */

public TreeDemo1() {

super();

initialize();

}

/**

 * This method initializes this

 *

 * @return void

 */

private void initialize() {

this.setSize(438, 309);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}

/**

 * This method initializes jContentPane

 *

 * @return javax.swing.JPanel

 */

private JPanel getJContentPane() {

if (jContentPane == null) {

GridLayout gridLayout = new GridLayout();

gridLayout.setRows(1);

gridLayout.setColumns(2);

jContentPane = new JPanel();

jContentPane.setLayout(gridLayout);

jContentPane.add(getjScrollPane(), null);

jContentPane.add(getJEditorPane(), null);

}

return jContentPane;

}

/**

 * This method initializes jTree

 *

 * @return javax.swing.JTree

 */

private JTree getJTree() {

if (jTree == null) {

top = new DefaultMutableTreeNode("黄山");

createNodes(top);

jTree = new JTree(top);

jTree.getSelectionModel().setSelectionMode(

TreeSelectionModel.SINGLE_TREE_SELECTION);

jTree.addTreeSelectionListener(this);

}

return jTree;

}

private void createNodes(DefaultMutableTreeNode top) {

DefaultMutableTreeNode jingdian = null, jiaotong = null, zhusu = null, binguan = null, luying = null, jiesu = null;

jingdian = new DefaultMutableTreeNode("景点");

jingdian.add(new DefaultMutableTreeNode("景点1"));

jingdian.add(new DefaultMutableTreeNode("景点2"));

jiaotong = new DefaultMutableTreeNode("交通");

jiaotong.add(new DefaultMutableTreeNode("路线1"));

jiaotong.add(new DefaultMutableTreeNode("路线2"));

jiaotong.add(new DefaultMutableTreeNode("路线3"));

zhusu = new DefaultMutableTreeNode("住宿");

zhusu.add(new DefaultMutableTreeNode("A住宿"));

zhusu.add(new DefaultMutableTreeNode("B住宿"));

zhusu.add(new DefaultMutableTreeNode("C住宿"));

binguan = new DefaultMutableTreeNode("宾馆");

binguan.add(new DefaultMutableTreeNode("X宾馆"));

binguan.add(new DefaultMutableTreeNode("Y宾馆"));

binguan.add(new DefaultMutableTreeNode("Z宾馆"));

binguan.add(new DefaultMutableTreeNode("XYZ宾馆"));

luying = new DefaultMutableTreeNode("露营");

luying.add(new DefaultMutableTreeNode("方式1"));

luying.add(new DefaultMutableTreeNode("方式2"));

jiesu = new DefaultMutableTreeNode("借宿");

jiesu.add(new DefaultMutableTreeNode("借宿1"));

jiesu.add(new DefaultMutableTreeNode("借宿2"));

jiesu.add(new DefaultMutableTreeNode("借宿3"));

top.add(jingdian);

top.add(jiaotong);

top.add(zhusu);

top.add(binguan);

top.add(luying);

top.add(jiesu);

}

/**

 * This method initializes jEditorPane

 *

 * @return javax.swing.JEditorPane

 */

private JEditorPane getJEditorPane() {

if (jEditorPane == null) {

jEditorPane = new JEditorPane();

}

return jEditorPane;

}

@Override

public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree

.getLastSelectedPathComponent();

if (node == null)

// Nothing is selected.

return;

if(node.isRoot()){

jEditorPane.setText("黄山的简介");

}

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exception e){

e.printStackTrace();

}

new TreeDemo1().setVisible(true);

}

});

}

}

java 如何生成三级树形菜单?

是要返回一个json串吧。mybytis里设置好要返回自定义的结果集合。就直接可以

java,jsp带checkbox的树形菜单,如何实现?

jsp中带有checkbox的属性菜单需要用easyui组件来实现,代码如下:

!DOCTYPE html

html

head

meta charset="UTF-8"

titleCheckBox Tree - jQuery EasyUI Demo/title

link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css"

link rel="stylesheet" type="text/css" href="../../themes/icon.css"

link rel="stylesheet" type="text/css" href="../demo.css"

script type="text/javascript" src="../../jquery.min.js"/script

script type="text/javascript" src="../../jquery.easyui.min.js"/script

/head

body

h2CheckBox Tree/h2

pTree nodes with check boxes./p

div style="margin:20px 0;"

a href="#" class="easyui-linkbutton" onclick="getChecked()"GetChecked/a

/div

div style="margin:10px 0"

input type="checkbox" checked onchange="$('#tt').tree({cascadeCheck:$(this).is(':checked')})"CascadeCheck

input type="checkbox" onchange="$('#tt').tree({onlyLeafCheck:$(this).is(':checked')})"OnlyLeafCheck

/div

div class="easyui-panel" style="padding:5px"

ul id="tt" class="easyui-tree" data-options="url:'tree_data1.json',method:'get',animate:true,checkbox:true"/ul

/div

script type="text/javascript"

function getChecked(){

var nodes = $('#tt').tree('getChecked');

var s = '';

for(var i=0; inodes.length; i++){

if (s != '') s += ',';

s += nodes[i].text;

}

alert(s);

}

/script

/body

/html

运行效果:

Java递归如何正确输出树形菜单

首先我们要建立树节点的类:

[java]

view

plain

copy

package

com.tree;

public

class

Node

{

private

Integer

id;

private

Integer

parentId;

private

String

name;

private

String

link;

public

Integer

getId()

{

return

id;

}

public

void

setId(Integer

id)

{

this.id

=

id;

}

public

Integer

getParentId()

{

return

parentId;

}

public

void

setParentId(Integer

parentId)

{

this.parentId

=

parentId;

}

public

String

getName()

{

return

name;

}

public

void

setName(String

name)

{

this.name

=

name;

}

public

String

getLink()

{

return

link;

}

public

void

setLink(String

link)

{

this.link

=

link;

}

}

输出树形菜单类:

[java]

view

plain

copy

package

com.tree;

import

java.util.ArrayList;

import

java.util.List;

public

class

Tree

{

private

StringBuffer

html

=

new

StringBuffer();

private

ListNode

nodes;

public

Tree(ListNode

nodes){

this.nodes

=

nodes;

}

public

String

buildTree(){

html.append("ul");

for

(Node

node

:

nodes)

{

Integer

id

=

node.getId();

if

(node.getParentId()

==

)

{

html.append("\r\nli

id='"

+

id

+

"'"

+

node.getName()+

"/li");

build(node);

}

}

html.append("\r\n/ul");

return

html.toString();

}

private

void

build(Node

node){

ListNode

children

=

getChildren(node);

if

(!children.isEmpty())

{

html.append("\r\nul");

for

(Node

child

:

children)

{

Integer

id

=

child.getId();

html.append("\r\nli

id='"

+

id

+

"'"

+

child.getName()+

"/li");

build(child);

}

html.append("\r\n/ul");

}

}

private

ListNode

getChildren(Node

node){

ListNode

children

=

new

ArrayListNode();

Integer

id

=

node.getId();

for

(Node

child

:

nodes)

{

if

(id.equals(child.getParentId()))

{

children.add(child);

}

}

return

children;

}

}

如何用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);

}

}

java树形菜单算法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现菜单树、java树形菜单算法的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。