① 树在java中的应用有哪些
首先:树与线性表、栈、队列等线性结构不同,树是一种非线性结构。一棵树只有一个根节点,如果一棵树有了多个根节点,那它已经不再是一棵树了,而是多棵树的集合,也被称为森林。
其次:java中树的应用主要有:菜单树,还有权限树,商品分类列表也是树结构。
② java 如何实现树、图结构
他们的实现是底层程序员早都写好了的,他们的原理确实是树、图结构。
③ java如何实现当点击添加或删除时能够现实左侧树形结构的实时更新
添加监听,当事件被触发 重新repaint()
④ 在Java中可以应用treeview控件吗
Java中不能用NET中的TreeView控件的,Java中用递归或者JQuery的插件可以做,再就是ExtJS的Tree,你需要的话扣扣我传给你。因为需要支持的JS库,这里发不出来,要什么样的就有什么样的。很强大。扣二五三七一七三七六
⑤ 如何用Java实现树形结构
[java] view plain
package com.tree.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args){
showTree();
}
public static void showTree(){
Connection conn=null;
ResultSet rs = null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/tree?user=root&password=root");
/*stmt=conn.createStatement();
rs=stmt.executeQuery("select * from country where pid=0");
while(rs.next()){
System.out.println(rs.getString("actile"));*/
tree(conn,0,0);
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void tree(Connection conn,int id,int level){
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
String sql = "select * from country where pid = " + id;
rs = stmt.executeQuery(sql);
while(rs.next()) {
StringBuffer strPre = new StringBuffer("");
for(int i=0; i<level; i++) {
strPre.append(" ");
}
System.out.println(strPre + rs.getString("actile"));
if(rs.getInt("is_leaf") != 0)
tree(conn, rs.getInt("id"), level + 1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库
[sql] view plain
create database tree;
use tree;
create table country
(
id int primary key auto_increment,
pid int,
actile varchar(40),
is_leaf int
);
insert into country values(1,0, '中国',1);
insert into country values(2,1,'北京',0);
insert into country values(3,0,'美国',1);
insert into country values(4,3,'纽约',0);
insert into country values(5,1,'浙江',1);
insert into country values(6,5,'杭州',1);
insert into country values(7,6,'滨江',0);
⑥ 初学java,如何把JTree组件添加到界面中
/**
*把代码复制到文件,可以运行。
*/
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Component;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.Rectangle;
importjava.awt.Transparency;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferedImage;
importjavax.swing.Icon;
importjavax.swing.ImageIcon;
importjavax.swing.JDialog;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTree;
importjavax.swing.UIManager;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.TreeCellRenderer;
/**
*
*@authorbeans
*/
publicclassTreeMain{
publicstaticvoidmain(String[]args){
newTreeMain().showDialog();
}
publicTreeMain(){
}
/**
*显示窗口
*/
privatevoidshowDialog(){
JDialogdialog=newJDialog();
dialog.setBounds(newRectangle(50,50,380,280));
dialog.setTitle("演示树");
dialog.addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
dialog.setVisible(false);
dialog.dispose();
}
});
dialog.add(this.getPanel(),BorderLayout.CENTER);
dialog.setVisible(true);
}
privateJPanelgetPanel(){
JPanelpanel=newJPanel();
JScrollPanetreePanel=newJScrollPane();
treePanel.setViewportView(this.getTree());
javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(panel);
panel.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel,javax.swing.GroupLayout.DEFAULT_SIZE,380,Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel,javax.swing.GroupLayout.PREFERRED_SIZE,272,javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18,Short.MAX_VALUE))
);
panel.add(treePanel,BorderLayout.CENTER);
returnpanel;
}
/**
*取得树。
*
*@return
*/
privateJTreegetTree(){
DefaultMutableTreeNoderoot=newDefaultMutableTreeNode();
root.add(this.getNode());
root.add(this.getNode());
root.add(this.getNode());
JTreejtree=newJTree(root);
jtree.setRootVisible(false);
jtree.setCellRenderer(newCTreeCellRenderer());
jtree.expandRow(1);
returnjtree;
}
/**
*取得树节点。
*
*@return
*/
(){
DefaultMutableTreeNodenode=newDefaultMutableTreeNode(newNodeObject(true,"节"));
for(inti=0;i<5;i++){
DefaultMutableTreeNodeleaf=newDefaultMutableTreeNode(newNodeObject(false,"叶"+i));
node.add(leaf);
}
returnnode;
}
/**
*树节点和树叶,关联对象。
*/
classNodeObject{
booleanisNode;
Stringname;
/**
*
*@paramisNodethevalueofisNode
*@paramnamethevalueofname
*/
NodeObject(booleanisNode,Stringname){
this.isNode=isNode;
this.name=name;
}
/**
*图标
*
*@paramisSelect选中节点时返回不同的图标。
*@return
*/
ImageIcongetIcon(booleanisSelect){
intwh=20;
BufferedImageimage=newBufferedImage(wh,wh,BufferedImage.TYPE_INT_ARGB);
Graphics2Dg2=image.createGraphics();
image=g2.getDeviceConfiguration().createCompatibleImage(wh,wh,Transparency.TRANSLUCENT);
Graphics2Dg2d=image.createGraphics();
Fontfont=newFont("Dialog",Font.PLAIN,wh-4);
g2d.setFont(font);
g2d.setBackground(Color.WHITE);
g2d.setColor(Color.BLACK);
g2d.drawString(isSelect?"S":"N",0,wh-1);
g2d.setColor(this.isNode?Color.RED:Color.YELLOW);
g2d.drawLine(0,5,wh,5);
g2d.drawLine(0,10,wh,10);
g2d.drawLine(0,15,wh,15);
g2d.dispose();
g2.dispose();
returnnewImageIcon(image);
}
StringgetName(){
returnthis.name;
}
}
/**
*树渲染器
*/
{
protectedColorm_textSelectionColor;
protectedColorm_textNonSelectionColor;
protectedColorm_bkSelectionColor;
protectedColorm_bkNonSelectionColor;
protectedColorm_borderSelectionColor;
protectedbooleanm_selected;
publicCTreeCellRenderer(){
m_textSelectionColor=UIManager.getColor("Tree.selectionForeground");
m_textNonSelectionColor=UIManager.getColor("Tree.textForeground");
m_bkSelectionColor=UIManager.getColor("Tree.selectionBackground");
m_bkNonSelectionColor=UIManager.getColor("Tree.textBackground");
m_borderSelectionColor=UIManager.getColor("Tree.selectionBorderColor");
}
@Override
(JTreetree,Objectvalue,
booleanselected,booleanexpanded,booleanleaf,introw,booleanhasFocus){
DefaultMutableTreeNodenode=(DefaultMutableTreeNode)value;
NodeObjectobj=(NodeObject)node.getUserObject();
this.setIcon(obj.getIcon(selected));
this.setText(""+obj.getName()+"");
this.setForeground(selected?m_textSelectionColor:m_textNonSelectionColor);
this.setBackground(selected?m_bkSelectionColor:m_bkNonSelectionColor);
this.m_selected=selected;
returnthis;
}
@Override
publicvoidpaint(Graphicsg){
ColorbColor=this.getBackground();
Iconicon=this.getIcon();
g.setColor(bColor);
intoffset=0;
if(icon!=null&&getText()!=null){
offset=(icon.getIconWidth()+this.getIconTextGap());
}
g.fillRect(offset,0,this.getWidth()-1-offset,this.getHeight()-1);
if(this.m_selected){
g.setColor(this.m_borderSelectionColor);
g.drawRect(offset,0,this.getWidth()-1-offset,this.getHeight()-1);
}
super.paint(g);
}
}
}
⑦ 如何用Java实现树形结构啊
定义一个简单的菜单类 这里是简单的示例 你可以自行扩展package entity;import java.util.ArrayList;
import java.util.List;/**
* 菜单类
* @author Administrator
*
*/
public class Menu {
/**
* 菜单标题
*/
private String title;
/**
* 子菜单的集合
*/
private List<Menu> childs;
/**
* 父菜单
*/
private Menu parent;
/**
* 构造函数 初始化标题和子菜单集合
*/
public Menu(String title) {
this();
this.title=title;
}
/**
* 构造函数 创建一个虚拟的父菜单(零级菜单) 所有的一级菜单都归属于一个虚拟的零级菜单
*
*/
public Menu() {
this.childs = new ArrayList<Menu>();
}
/**
* 获取子菜单
* @return
*/
public List<Menu> getChilds() {
return childs;
}
/**
* 获取标题
* @return
*/
public String getTitle() {
return title;
}
/**
* 获取父菜单
* @return
*/
public Menu getParent() {
return parent;
}
/**
* 添加子菜单并返回该子菜单对象
* @param child
* @return
*/
public Menu addChild(Menu child){
this.childs.add(child);
return child;
}
/**
* 设置父菜单
* @param parent
*/
public void setParent(Menu parent) {
this.parent = parent;
}
/**
* 设置标题
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
} 测试package entity;
/**
* 测试类
* @author Administrator
*
*/
public class Test { /**
* @param args
*/
public static void main(String[] args) {
/**
* 创建一个虚拟的父菜单 用于存放一级菜单 menu01 和 menu02
*/
Menu root = new Menu();
/**
* 创建两个一级菜单
*/
Menu menu01 = new Menu("一级菜单01");
Menu menu02 = new Menu("一级菜单02");
/**
* 加入虚拟菜单
*/
root.addChild(menu01);
root.addChild(menu02);
/**
* 为两个一级菜单分别添加两个子菜单 并返回该子菜单 需要进一步处理的时候 才接收返回的对象 否则只要调用方法
*/
Menu menu0101 = menu01.addChild(new Menu("二级菜单0101"));
menu01.addChild(new Menu("二级菜单0102"));
menu02.addChild(new Menu("二级菜单0201"));
Menu menu0202 = menu02.addChild(new Menu("二级菜单0202"));
/**
* 添加三级菜单
*/
menu0101.addChild(new Menu("三级菜单010101"));
menu0202.addChild(new Menu("三级菜单020201"));
/**
* 打印树形结构
*/
showMenu(root);
} /**
* 递归遍历某个菜单下的菜单树
*
* @param menu
* 根菜单
*/
private static void showMenu(Menu menu) {
for (Menu child : menu.getChilds()) {
showMenu(child, 0);
}
} private static void showMenu(Menu menu, int tabNum) {
for (int i = 0; i < tabNum; i++)
System.out.print("\t");
System.out.println(menu.getTitle());
for (Menu child : menu.getChilds())
// 递归调用
showMenu(child, tabNum + 1);
}}
控制台输出结果 一级菜单01 二级菜单0101
三级菜单010101
二级菜单0102一级菜单02
二级菜单0201
二级菜单0202
三级菜单020201
⑧ Java中有没有现成的树形结构的类
树时用来存储东西的,如果非要说类似的类,那么应该是treemap和treeset应该是使用的avl平衡二叉树实现的。其他的,好像暂时没有发现。正常算法使用的树,都是用的node里面存放引用来实现的。