新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java树形控件代码 什么是树形控件

哪位大侠可以帮我写一段java代码!是关于根据数据库里的数据来形成一个动态树形图

这是代码,你可以自己调试一下。

三江侗ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

数据结构如下:

CREATE TABLE dtree (

id int,

pid int,

name varchar(200),

url varchar(200),

title varchar(200),

target varchar(200),

icon varchar(200),

iconopen varchar(200),

opened bit);

为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。

DTree类代码如下:

package work3;

public class DTree {

private int id;

private int pid;

private String name;

private String url;

private String title;

private String target;

private String icon;

private String iconOpen;

private int opened;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public int getPid() {

return pid;

}

public void setPid(int pid) {

this.pid = pid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getTarget() {

return target;

}

public void setTarget(String target) {

this.target = target;

}

public String getIcon() {

return icon;

}

public void setIcon(String icon) {

this.icon = icon;

}

public String getIconOpen() {

return iconOpen;

}

public void setIconOpen(String iconOpen) {

this.iconOpen = iconOpen;

}

public int getOpened() {

return opened;

}

public void setOpened(int opened) {

this.opened = opened;

}

}

work3.jsp代码如下:

%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%

%@ page import="java.sql.*"%

jsp:useBean id='settree' scope="application" class="work3.DTree" /

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

""

html

head

meta http-equiv="Content-Type" content="text/html; charset=GB18030"

link rel="StyleSheet" href="dtree.css" type="text/css" /

script type="text/javascript" src="dtree.js"/script

titledTree in MySQL/title

/head

body

h2

Example

/h2

div class="dtree"

p

a href="javascript: d.openAll();"open all/a |

a href="javascript: d.closeAll();"close all/a

/p

script type="text/javascript"

!--

d = new dTree('d');

%

//驱动程序名

String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

//数据库用户名

String userName = "sa";

//密码

String userPwd = "1";

//数据库名

String dbName = "master";

//表名

String tableName = "dtree";

//连接字符串

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;

//加载驱动

Class.forName(driverName).newInstance();

//连接数据库

java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);

//得到Statement实例

java.sql.Statement statement = conn.createStatement();

//查询数据

String sql = "select * from " + tableName;

//返回结果

java.sql.ResultSet rs = statement.executeQuery(sql);

//获取变量

while (rs.next()) {

settree.setId(rs.getInt(1));

settree.setPid(rs.getInt(2));

settree.setName(rs.getString(3));

settree.setUrl(rs.getString(4));

settree.setTitle(rs.getString(5));

settree.setTarget(rs.getString(6));

settree.setIcon(rs.getString(7));

settree.setIconOpen(rs.getString(8));

settree.setOpened(rs.getInt(9));

if(settree.getPid()==0)

settree.setOpened(1);

%

d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);

%

}

%

document.write(d);

//--

/script

/div

/body

/html

用Java实现一个树形结构,并对其进行遍历

import java.util.Iterator;

import java.util.Random;

import java.util.TreeSet;

public class Demo{

public static void main(String[] args) throws Exception {

TreeSetInteger ts = new TreeSetInteger();

for(int i = 0; i  10; i++){

ts.add(new Random().nextInt(999));

}

for(IteratorInteger it = ts.iterator(); it.hasNext();){

System.out.println(it.next());

}

}

}

//上面是利用TreeSet进行简单的二叉树实现,另有遍历,当然遍历是自然顺序。

//如有需要请自行修改吧。

如何用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树形控件

我写过,你要的话可以发给你,可以后台联系,为了符合回答规范,还是把代码贴出来(鉴于篇幅仅帖一个文件):

package ui;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

public class TreePanel extends JPanel {

private VectorTreePanelNode nodes;

private VectorInteger selectedNodes;

private int tmpX, tmpY;

private int editingNode = -1;

private int addingParent = -2;

private int mouseX, mouseY;

public TreePanel () {

nodes = new VectorTreePanelNode();

selectedNodes = new VectorInteger();

setBackground(Color.WHITE);

addMouseListener(new MouseAdapter() {

public void mousePressed (MouseEvent me) {

tmpX = me.getX();

tmpY = me.getY();

if (nodes.size() == 0) {

addingParent = -1;

}

int i = 0;

for (; inodes.size(); i++) {

if (nodes.get(i).getBounds().contains(mouseX, mouseY)) {

if (me.isControlDown()) {

if (!selectedNodes.contains(i))

selectedNodes.add(i);

else

selectedNodes.removeElement(i);

} else if (!selectedNodes.contains(i)) {

selectedNodes = new VectorInteger();

selectedNodes.add(i);

}

if (me.getClickCount() == 2) {

nodes.get(i).setValue("");

editingNode = i;

}

break;

}

}

if (i == nodes.size())

if (!me.isControlDown())

selectedNodes = new VectorInteger();

repaint();

}

public void mouseReleased (MouseEvent me) {

addingParent = -2;

}

});

addMouseMotionListener(new MouseMotionAdapter() {

public void mouseDragged (MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

int i = 0;

for (; iselectedNodes.size(); i++) {

if (nodes.get(selectedNodes.get(i)).getBounds().contains(mouseX, mouseY))

break;

}

if (i != selectedNodes.size()) {

if (me.isAltDown() addingParent != -3) {

addingParent = selectedNodes.get(i);

} else {

for (i=0; iselectedNodes.size(); i++) {

nodes.get(selectedNodes.get(i)).setLocation(nodes.get(selectedNodes.get(i)).getLocation().x + mouseX - tmpX,

nodes.get(selectedNodes.get(i)).getLocation().y + mouseY - tmpY);

}

tmpX = mouseX;

tmpY = mouseY;

}

}

repaint();

}

public void mouseMoved (MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

}

});

addKeyListener(new KeyAdapter() {

public void keyTyped (KeyEvent ke) {

if (editingNode != -1) {

if (ke.getKeyCode() == KeyEvent.VK_ENTER)

editingNode = -1;

else

nodes.get(editingNode).setValue(nodes.get(editingNode).getValue().toString()+ke.getKeyChar());

} else if (ke.getKeyCode() == KeyEvent.VK_DELETE) {

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

nodes.remove(selectedNodes.get(i));

}

repaint();

}

});

}

public void paint (Graphics g) {

super.paint(g);

FontMetrics fm = g.getFontMetrics();

if (addingParent -2) {

String str = "node" + nodes.size();

int width = fm.stringWidth(str);

int height = fm.getHeight();

TreePanelNode pnt = null;

if (addingParent != -1)

pnt = nodes.get(addingParent);

nodes.add(new TreePanelNode(pnt, mouseX-width/2-10, mouseY-height/2-20, width+20, height+40, str));

addingParent = -3;

selectedNodes = new VectorInteger();

selectedNodes.add(nodes.size()-1);

}

for (int i=0; inodes.size(); i++) {

String str = nodes.get(i).getValue().toString();

int width = fm.stringWidth(str);

int height = fm.getHeight();

nodes.get(i).setSize(width + 20, height + 40);

if (selectedNodes.contains(i))

g.setColor(Color.RED);

else

g.setColor(Color.BLACK);

if (nodes.get(i).getParent() != null)

g.drawLine(nodes.get(i).getLocation().x+nodes.get(i).getWidth()/2,

nodes.get(i).getLocation().y+nodes.get(i).getHeight()/2,

nodes.get(i).getParent().getLocation().x+nodes.get(i).getParent().getWidth()/2,

nodes.get(i).getParent().getLocation().y+nodes.get(i).getParent().getHeight()/2);

g.drawString(str, nodes.get(i).getLocation().x + 10, nodes.get(i).getLocation().y + 20);

g.drawRect(nodes.get(i).getLocation().x, nodes.get(i).getLocation().y, nodes.get(i).getWidth(), nodes.get(i).getHeight());

}

grabFocus();

}

}


网站标题:java树形控件代码 什么是树形控件
文章源于:http://wjwzjz.com/article/hpiiso.html
在线咨询
服务热线
服务热线:028-86922220
TOP