java操作树形菜单代码 实现树形菜单

用java 生成一个树形菜单

package com.csii;

创新互联公司是一家专注于成都做网站、成都网站制作与策划设计,华蓥网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:华蓥等地区。华蓥做网站价格咨询:18982081108

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,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实现树形结构啊?

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操作树形菜单代码 实现树形菜单
文章起源:http://scyanting.com/article/hidcsd.html