① 樹在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裡面存放引用來實現的。