树状图java代码 java 树状图
哪位大侠可以帮我写一段java代码!是关于根据数据库里的数据来形成一个动态树形图
这是代码,你可以自己调试一下。
公司主营业务:做网站、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出沙坪坝免费做网站回馈大家。
数据结构如下:
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 编写一个程序,在命令行中以树状结构展现特定的文件夹及其子文件(夹)!
当然在理论上是可以实现的,可以将所有的子文件都以树形结构出来,但是文件很多的时候就会非常纠结
我理解中的树形结构大概是这样(不知道这样的图形是不是你想要的)
a
|
------------------
| | |
b c d
以下是代码,找了系统盘下子文件较少的文件夹 C:/Windows/AppPatch,当然也可以换成你自己的路径来测试
import java.io.File;
public class FileTree {
/**
* @param args
*/
public static void main(String[] args) {
try{
File file = new File("C:\\Windows\\AppPatch");
if(file.isDirectory()){
String[] fileList = file.list();
String fileName = file.getName();
int allLength = 0;
for(int i=0;ifileList.length;i++){
allLength += (fileList[i]+" ").length();
}
for(int i=0;iallLength/2;i++){
System.out.print(" ");
}
System.out.println(fileName);
for(int i=0;iallLength/2;i++){
System.out.print(" ");
}
for(int i=0;ifileName.length()/2;i++){
System.out.print(" ");
}
System.out.println("|");
for(int i=0;iallLength;i++){
System.out.print("-");
}
System.out.println("");
for(int i=0;ifileList.length;i++){
int tmpLength = fileList[i].length();
int subLength = tmpLength/2;
int lastLength = tmpLength - subLength - 1;
for(int j=0;jsubLength;j++){
System.out.print(" ");
}
System.out.print("|");
for(int j=0;jlastLength;j++){
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println("");
for(int i=0;ifileList.length;i++){
System.out.print(fileList[i]+" ");
}
}
else{
System.out.println("对不起,你提供的路径不是文件夹");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这时可以发现输出每一个子文件/子文件夹的名字已经比较长,要是再想输出这些子文件夹里面的文件,那幅图个人觉得相当纠结,也许是我水平没够吧或是我理解错了你说的树形结构
希望以上代码对你有帮助
请问这个树状图在Java中应该怎么做?
打个比方
可以写一个Tree的类
public class Tree extends BaseDomain {
private String id;
private String key;
private String icon;
private String title;
private String value;
private String text;
private String code;
private String mtype;
private String type;
/**
* 部门的特殊个别字段
* 1.部门 / 2.岗位 的编码
*/
private String number;
private Double order;
private Double sort;
private String href;
private String component;
private ListTreeT children;
private String parentId;
private boolean hasParent = false;
private boolean hasChildren = false;
private Date createTime;
private Date modifyTime;
public void initChildren(){
this.children = new ArrayList();
}
}
在写一个工具类
public class TreeUtil {
protected TreeUtil() {
}
private final static String TOP_NODE_ID = "0";
/**
* 用于构建菜单
*
* @param nodes nodes
* @param T T
* @return T TreeT
*/
public static T TreeT build(ListTreeT nodes) {
if (nodes == null) {
return null;
}
ListTreeT topNodes = new ArrayList();
nodes.forEach(node - {
String pid = node.getParentId();
if (pid == null || TOP_NODE_ID.equals(pid)) {
topNodes.add(node);
return;
}
for (TreeT n : nodes) {
String id = n.getId();
if (id != null id.equals(pid)) {
if (n.getChildren() == null)
n.initChildren();
n.getChildren().add(node);
node.setHasParent(true);
n.setHasChildren(true);
n.setHasParent(true);
return;
}
}
if (topNodes.isEmpty())
topNodes.add(node);
});
TreeT root = new Tree();
root.setId("0");
root.setParentId("");
root.setHasParent(false);
root.setHasChildren(true);
root.setChildren(topNodes);
root.setText("root");
return root;
}
}
写完了这两个在写业务层
一个构建书的方法
private void buildTrees(List trees, List menus, List ids) {
menus.forEach(menu - {
ids.add(menu.getId().toString());
Tree tree = new Tree();
tree.setId(menu.getId().toString());
tree.setKey(tree.getId());
tree.setParentId(menu.getParentId().toString());
tree.setText(menu.getName());
tree.setTitle(tree.getText());
tree.setIcon(menu.getIcon());
tree.setComponent(menu.getComponent());
tree.setCreateTime(menu.getCreateTime());
tree.setCreateTime(menu.getCreateTime());
tree.setHref(menu.getHref());
tree.setSort(menu.getSort());
tree.setCode(menu.getCode());
tree.setMtype(menu.getMtype());
trees.add(tree);
});
}
MapString, Object result = new HashMap();
ListDept depts = findDepts(dept, request);
ListTreeDept trees = new ArrayList();
buildTrees(trees, depts);
TreeDept deptTree = TreeUtil.build(trees);
result.put("rows", deptTree);
result.put("total", depts.size());
求 java 刷新 JScrollPane中jtree树状图的方法
java 中actionPerformed的方法可以刷新jtree树状图的,具体做法是在actionPerformed的方法尾部加上JSPcrollPanel.updateUI();
代码如下:
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeDemo {
public static void main(String[] args) {
// 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。
// public DefaultMutableTreeNode(Object userObject)
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部");
node1.add(new DefaultMutableTreeNode(new User("小花")));
node1.add(new DefaultMutableTreeNode(new User("小虎")));
node1.add(new DefaultMutableTreeNode(new User("小龙")));
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部");
node2.add(new DefaultMutableTreeNode(new User("小叶")));
node2.add(new DefaultMutableTreeNode(new User("小雯")));
node2.add(new DefaultMutableTreeNode(new User("小夏")));
DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理");
top.add(new DefaultMutableTreeNode(new User("总经理")));
top.add(node1);
top.add(node2);
final JTree tree = new JTree(top);
JFrame f = new JFrame("JTreeDemo");
f.add(tree);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (node == null)
return;
Object object = node.getUserObject();
if (node.isLeaf()) {
User user = (User) object;
System.out.println("你选择了:" + user.toString());
}
}
});
}
}
class User {
private String name;
public User(String n) {
name = n;
}
// 重点在toString,节点的显示文本就是toString
public String toString() {
return name;
}
}
本文标题:树状图java代码 java 树状图
本文链接:http://scyanting.com/article/docdsgo.html