當前位置:ag真人国际官网-ag旗舰厅官方网站 » 編程語言 » java的樹

java的樹-ag真人国际官网

發布時間: 2022-01-08 05:35:05

java畫樹

畫什麼都行,這個你得自己實現。我可以給你寫一個畫樹的小程序:

---------------------------------------------------------
//treenode.java

package util;

import java.util.vector;

public class treenode {
private vector children;
private treenode parent;
private treenode root;

public treenode (treenode parent) {
children = new vector();
if (parent == null)
root = this;
else {
parent.addchild(this);
}
}

public void addchild (treenode n) {
addchild(children.size(), n);
}

public void addchild (int index, treenode n) {
children.add(index, n);
n.parent = this;
}

public void removechild (treenode n) {
children.remove(n);
}

public void removechild (int index) {
children.remove(index);
}

public void setchild (int index, treenode n) {
children.set(index, n);
n.parent = this;
}

public treenode getchild (int index) {
return children.get(index);
}

public void changeposition (int index1, int index2) {
if (0 <= index1 && index1 < children.size() &&
0 <= index2 && index2 <= children.size()) {
treenode tmp = children.get(index1);
children.set(index1, children.get(index2));
children.set(index2, tmp);
}
}

public void setparent (treenode n) {
parent = n;
}

public treenode getroot () {
return root;
}

public treenode getparent () {
return parent;
}

public vector getchildren () {
return children;
}
}

------------------------------------------------------------
//treepanelnode.java

package ui;

import util.treenode;
import java.awt.*;

public class treepanelnode extends treenode {
private int x, y, width, height;
private object value;
private treepanelnode parent;

public treepanelnode (treepanelnode parent, int x, int y, int width, int height, object value) {
super(parent);
this.x = x;
this.y = y;
this.parent = parent;
this.width = width;
this.height = height;
this.value = value;
}

public void setlocation (int x, int y) {
this.x = x;
this.y = y;
}

public void setsize (int width, int height) {
this.width = width;
this.height = height;
}

public rectangle getbounds () {
return new rectangle(x, y, width, height);
}

public object getvalue () {
return value;
}

public point getlocation () {
return new point(x, y);
}

public int getwidth () {
return width;
}

public int getheight () {
return height;
}

public void setvalue (object value) {
this.value = value;
}

public treepanelnode getparent () {
return parent;
}
}

--------------------------------------------------------------
//treepanel.java

package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.vector;

public class treepanel extends jpanel {
private vector nodes;
private vector selectednodes;

private int tmpx, tmpy;
private int editingnode = -1;
private int addingparent = -2;
private int mousex, mousey;

public treepanel () {
nodes = new vector();
selectednodes = new vector();

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 (; 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 vector();
selectednodes.add(i);
}
if (me.getclickcount() == 2) {
nodes.get(i).setvalue("");
editingnode = i;
}
break;
}
}
if (i == nodes.size())
if (!me.iscontroldown())
selectednodes = new vector();
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 (; 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; 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());
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 vector();
selectednodes.add(nodes.size()-1);
}
for (int i=0; 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();
}
}

----------------------------------------------------------
//mainview.java

package ui;

import javax.swing.*;

public class mainview extends jframe {
private treepanel tp;

public mainview () {
super("treedemo");
setdefaultcloseoperation(jframe.exit_on_close);
setsize(800, 600);
setlocationrelativeto(null);

tp = new treepanel();
add(tp);
setvisible(true);
}

public static void main (string args[]) {
new mainview();
}
}

------------------------------------------------------------
好了。然後你新建一個 util 文件夾 和 一個 ui 文件夾,把第一個文件放在 util 文件夾下,把其餘文件放在 ui 文件夾下,文件名都根據我的注釋就行了。
例如你這兩個文件夾新建在 d 盤下,在命令行中編譯運行:

----------------------------------------------------------
...>d:

d:\>javac util\treenode.java ui\treepanelnode.java ui\treepanel.java ui\mainview.java

d:\>java ui.mainview

----------------------------------------------------------
於是便能看到效果。祝您好運,順便我寫了這么多就給個最佳答案吧,哈哈。
忘了告訴你,按住 alt 鍵拖動 node 可以添加子節點。
按住 ctrl 鍵單擊 node 可以多選。
雙擊一個 node 然後可以從鍵盤輸入 新的字元串,按回車結束。

----------------------------------------------------------
如果對以上的程序還有什麼問題,留下郵箱,我直接發給你。

❷ java中的樹

swing里有jtree啊,好多實現類
web版本里也有很多現成的js 如 dtree

❸ 樹在java中的應用有哪些

首先:樹與線性表、棧、隊列等線性結構不同,樹是一種非線性結構。一棵樹只有一個根節點,如果一棵樹有了多個根節點,那它已經不再是一棵樹了,而是多棵樹的集合,也被稱為森林。
其次:java中樹的應用主要有:菜單樹,還有許可權樹,商品分類列表也是樹結構。

❹ 關於java樹結構的實現

可以用遞歸模擬樹
要求子樹擁有父樹的id;
絕對原創;
import java.util.arraylist;
import java.util.list;

public class test2 {
public static void main(string[]args){
list trees = new arraylist();
int id = 1;
tree t1 = new tree(0,id ,"我是根樹");
tree t2 = new tree(0,id ,"我是第二個根樹");
tree t3 = new tree(1,id ,"我是子樹");
trees.add(t1);
trees.add(t2);
trees.add(t3);

tree t4 = new tree(1,id ,"樹根你好");
tree t5 = new tree(4,id ,"我不是樹根");
tree t6 = new tree(5,id ,"我才是樹根");
trees.add(t4);
trees.add(t5);
trees.add(t6);

show(trees);
}

public static void show(list trees){
for(int i=0;i tree t = trees.get(i);
if(t.parent == 0){
stringbuffer blank = new stringbuffer();
t.show(trees,blank);
}
}
}
}

import java.util.list;

public class tree {
public tree(int parent,int id,string str) {
this.parent = parent;
this.id = id;
this.str = str;
}
int parent;//樹的根樹
int id;
string str;
// stringbuffer blank = new stringbuffer();
void show(list trees, stringbuffer blank){
blank.append(" ");
system.out.println(blank str );
for(int i=0;i tree t = trees.get(i);
if(t.parent == id){
t.show(trees,blank);
}
}
}

}

❺ java 如何實現樹、圖結構

他們的實現是底層程序員早都寫好了的,他們的原理確實是樹、圖結構。

❻ java樹結構

請問java中有沒有實現tree型結構的類(如鏈表在java中可以用list實現),是可以用遞歸模擬樹要求子樹擁有父樹的id; 絕對原創; import java.util.array

❼ java對象樹是什麼東東

給你這么講吧:就相當於一個大家庭一樣,首先是祖父母,外祖父母,然後是線面的父母,叔叔,阿姨等,再然後是表哥,表姐,堂兄堂弟等,再然後是下面的子子孫孫.等等.就如同一顆倒置的大樹,根在上邊,枝葉在下邊一樣,有若干分支.但不是無窮無盡的.能明白嗎?

❽ 如何用java實現樹形結構啊

定義一個簡單的菜單類 這里是簡單的示例 你可以自行擴展package entity;import java.util.arraylist;
import java.util.list;/**
* 菜單類
* @author administrator
*
*/
public class menu {
/**
* 菜單標題
*/
private string title;
/**
* 子菜單的集合
*/
private list

childs;

/**
* 父菜單
*/
private menu parent;

/**
* 構造函數 初始化標題和子菜單集合
*/
public menu(string title) {
this();
this.title=title;
}
/**
* 構造函數 創建一個虛擬的父菜單(零級菜單) 所有的一級菜單都歸屬於一個虛擬的零級菜單
*
*/
public menu() {
this.childs = new arraylist();
}
/**
* 獲取子菜單
* @return
*/
public list 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中如何建立一個java樹,請詳解

importjava.awt.*;
importjavax.swing.*;
classtreedemoextendsjframe
{
publictreedemo()
{
setsize(400,300);
settitle("演示怎樣使用jtree");
show();
jscrollpanejpanel=newjscrollpane();
getcontentpane().add(jpanel);
jtreejtree=newjtree();
jpanel.getviewport().add(jtree,null);
validate();
setdefaultcloseoperation(jframe.exit_on_close);
}
}
publicclassexample5_25
{
publicstaticvoidmain(string[]args)
{
treedemoframe=newtreedemo();
}
}

其中jscrollpane是一個帶滾動條的面板類。

將對象加入到帶滾動條的面板類中,在將已建的數放入到其中。

就可建立一個系統默認的樹結構。

❿ java中有沒有現成的樹形結構的類

樹時用來存儲東西的,如果非要說類似的類,那麼應該是treemap和treeset應該是使用的avl平衡二叉樹實現的。其他的,好像暫時沒有發現。正常演算法使用的樹,都是用的node裡面存放引用來實現的。

熱點內容
phpjava交互 發布:2024-07-17 16:58:57 瀏覽:356
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
网站地图