當前位置:首頁 » 編程語言 » treejava

treejava

發布時間: 2022-07-20 01:31:49

java樹組件JTree的用法,要求詳解啊!!

樹都是由節點組成的.並且一顆樹只有一個根節點,下面是代碼運行的javapackage demo1;import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;public class Application1 {
boolean packFrame = false; /**
* Construct and show the application.
*/
public Application1() {
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
} // Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
} /**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exception) {
exception.printStackTrace();
} new Application1();
}
});
}
}package demo1;
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.BorderFactory;public class Frame1
extends JFrame {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JTree jTree1;
BorderLayout borderLayout1 = new BorderLayout();

public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(465, 380));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(37, 12, 216, 317));
jPanel1.setLayout(borderLayout1);
contentPane.add(jPanel1);
DefaultMutableTreeNode root=new DefaultMutableTreeNode("根節點");
DefaultMutableTreeNode r1=new DefaultMutableTreeNode("子節點");
DefaultMutableTreeNode r01=new DefaultMutableTreeNode("子節點下的節點");
DefaultMutableTreeNode r2=new DefaultMutableTreeNode("子節點2");
DefaultMutableTreeNode r3=new DefaultMutableTreeNode("子節點三");
// 向根節點中添加子節點
root.add(r1);
root.add(r2);
r1.add(r01);
root.add(r3);
jTree1=new JTree(root);
jPanel1.add(jTree1, java.awt.BorderLayout.NORTH);
//傳入根節點
this.setVisible(true);
this.setBounds(100,100,400,500); }
public static void main(String[] args) {
Frame1 f=new Frame1();
}
}

❷ java中Tree怎麼給子結點設置監聽

給葉節點設置監聽的Java程序如下:
package Tree.java;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeWin extends JFrame implements TreeSelectionListener{
JTree tree;
TreeWin()
{
setSize(300,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根結點");
DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("結點");
DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("結點");
DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode("葉結點");
DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode("葉結點");
root.add(t1);
root.add(t2);
t1.add(t1_1);
t2.add(t1_2);
tree= new JTree(root); //創建根為root的樹
tree.addTreeSelectionListener(this);//給整個樹加選擇監聽
JScrollPane scrollPane = new JScrollPane(tree);
con.add(scrollPane);
}
public static void main(String[] args) {
TreeWin win = new TreeWin();
win.setVisible(true);
}
public void valueChanged(TreeSelectionEvent tse) {
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selectionNode.isLeaf()){ //葉子節點的監聽
System.out.println(selectionNode.toString());
}
}
}

❸ java的JTree的使用方法,

JTree組件被用來以樹的形式顯示數據,一層套一層,看起來清晰明了,使用戶很方便地就了解到各個節點之間的層次關系,可以很容易地找到相關的數據。

例如 Windows 系統的文件管理器,它就是一個典型的樹層次結構。


JTree的構造函數:

·JTree() 建立一棵默認的樹。

·JTree(Hashtable data) 利用Hashtable建立樹。

·JTree(Object[] data) 利用數組建立樹。

·JTree(TreeModel model) 利用TreeModel建立樹。

·JTree(TreeNode root) 建立以root為根的樹。

·JTree(TreeNode root,Boolean children) 建立以 root 為根的樹,並設定是否允許有子節點。

·JTree(Vector value) 利用Vector建立樹。


使用一個JTree可以簡單地像下面這樣表示:


importjavax.swing.JFrame;
importjavax.swing.JTree;
importjavax.swing.event.TreeSelectionEvent;
importjavax.swing.event.TreeSelectionListener;
importjavax.swing.tree.DefaultMutableTreeNode;

publicclassTreeDemo{
publicstaticvoidmain(String[]args){

//創建沒有父節點和子節點、但允許有子節點的樹節點,並使用指定的用戶對象對它進行初始化。
//publicDefaultMutableTreeNode(ObjectuserObject)
DefaultMutableTreeNodenode1=newDefaultMutableTreeNode("軟體部");
node1.add(newDefaultMutableTreeNode(newUser("小花")));
node1.add(newDefaultMutableTreeNode(newUser("小虎")));
node1.add(newDefaultMutableTreeNode(newUser("小龍")));

DefaultMutableTreeNodenode2=newDefaultMutableTreeNode("銷售部");
node2.add(newDefaultMutableTreeNode(newUser("小葉")));
node2.add(newDefaultMutableTreeNode(newUser("小雯")));
node2.add(newDefaultMutableTreeNode(newUser("小夏")));

DefaultMutableTreeNodetop=newDefaultMutableTreeNode("職員管理");

top.add(newDefaultMutableTreeNode(newUser("總經理")));
top.add(node1);
top.add(node2);
finalJTreetree=newJTree(top);
JFramef=newJFrame("JTreeDemo");
f.add(tree);
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加選擇事件
tree.addTreeSelectionListener(newTreeSelectionListener(){

@Override
publicvoidvalueChanged(TreeSelectionEvente){
DefaultMutableTreeNodenode=(DefaultMutableTreeNode)tree
.getLastSelectedPathComponent();

if(node==null)
return;

Objectobject=node.getUserObject();
if(node.isLeaf()){
Useruser=(User)object;
System.out.println("你選擇了:"+user.toString());
}

}
});
}
}

classUser{
privateStringname;

publicUser(Stringn){
name=n;
}

//重點在toString,節點的顯示文本就是toString
publicStringtoString(){
returnname;
}
}

❹ java中的tree是什麼意思

樹狀結構,常用於菜單,或是文件管理等等具有層次關系的內容;

❺ Java怎麼實現輸出是一個tree結構

樹節點類:

packagecn.com.tree;

publicclassNode{
privateIntegerid;
privateIntegerparentId;
privateStringname;
privateStringlink;

publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicIntegergetParentId(){
returnparentId;
}
publicvoidsetParentId(IntegerparentId){
this.parentId=parentId;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetLink(){
returnlink;
}
publicvoidsetLink(Stringlink){
this.link=link;
}
}

輸出樹形菜單類:

packagecn.com.tree;

importjava.util.ArrayList;
importjava.util.List;

publicclassTree{
privateStringBufferhtml=newStringBuffer();
privateList<Node>nodes;

publicTree(List<Node>nodes){
this.nodes=nodes;
}

publicStringbuildTree(){
html.append("<ul>");
for(Nodenode:nodes){
Integerid=node.getId();
if(node.getParentId()==null){
html.append(" <liid='"+id+"'>"+node.getName()+"</li>");
build(node);
}
}
html.append(" </ul>");
returnhtml.toString();
}

privatevoidbuild(Nodenode){
List<Node>children=getChildren(node);
if(!children.isEmpty()){
html.append(" <ul>");
for(Nodechild:children){
Integerid=child.getId();
html.append(" <liid='"+id+"'>"+child.getName()+"</li>");
build(child);
}
html.append(" </ul>");
}
}

privateList<Node>getChildren(Nodenode){
List<Node>children=newArrayList<Node>();
Integerid=node.getId();
for(Nodechild:nodes){
if(id.equals(child.getParentId())){
children.add(child);
}
}
returnchildren;
}
}

測試類:

packagezzj.test;

importjava.util.ArrayList;
importjava.util.List;

importcn.com.tree.Node;
importcn.com.tree.Tree;


publicclassTest{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
List<Node>nodes=newArrayList<Node>();

Nodenode1=newNode();
node1.setId(1);
node1.setName("node1");
node1.setParentId(null);
node1.setLink(null);
nodes.add(node1);

Nodenode11=newNode();
node11.setId(11);
node11.setName("node11");
node11.setParentId(1);
node11.setLink(null);
nodes.add(node11);

Nodenode111=newNode();
node111.setId(111);
node111.setName("node111");
node111.setParentId(11);
node111.setLink(null);
nodes.add(node111);

Nodenode12=newNode();
node12.setId(12);
node12.setName("node12");
node12.setParentId(1);
node12.setLink(null);
nodes.add(node12);

Nodenode2=newNode();
node2.setId(2);
node2.setName("node2");
node2.setParentId(null);
node2.setLink(null);
nodes.add(node2);

Nodenode21=newNode();
node21.setId(21);
node21.setName("node21");
node21.setParentId(2);
node21.setLink(null);
nodes.add(node21);

Nodenode3=newNode();
node3.setId(3);
node3.setName("node3");
node3.setParentId(null);
node3.setLink(null);
nodes.add(node3);

Treetree=newTree(nodes);
System.out.println(tree.buildTree());
}
}

❻ java tree除了在url加參數外,如何傳參數

拼url就行了啊 。如果你get的地址是<%=path %>/admin/getUsers
那就改成<%=path %>/admin/getUsers?id=rows.id 這樣去請求就行。

❼ java(演算法與數據結構)tree

代碼實現[一]部分
package ChapterEight;

class Tree {
class Node {
public long value;

public Node leftChild;

public Node rightChild;

public Node(long value) {
this.value = value;
leftChild = null;
rightChild = null;
}
}

public Node root;

public Tree() {
root = null;
}

// 向樹中插入一個節點
public void insert(long value) {
Node newNode = new Node(value);
// 樹是空的
if (root == null)
root = newNode;
else {
Node current = root;
Node parentNode;
while (true) {
parentNode = current;
if (value < current.value) {
current = current.leftChild;
// 要插入的節點為左孩子節點
if (current == null) {
parentNode.leftChild = newNode;
return;
}
} else {
// 要插入的節點為右孩子節點
current = current.rightChild;
if (current == null) {
parentNode.rightChild = newNode;
return;
}
}
}
}
}

// 先續遍歷樹中的所有節點
public void preOrder(Node currentRoot) {
if (currentRoot != null) {
System.out.print(currentRoot.value + " ");
preOrder(currentRoot.leftChild);
preOrder(currentRoot.rightChild);
}
}

// 中續遍歷樹中的所有節點
public void inOrder(Node currentNode) {
if (currentNode != null) {
inOrder(currentNode.leftChild);
System.out.print(currentNode.value + " ");
inOrder(currentNode.rightChild);
}
}

// 後續遍歷樹中的所有節點
public void postOrder(Node currentNode) {
if (currentNode != null) {
postOrder(currentNode.leftChild);
postOrder(currentNode.rightChild);
System.out.print(currentNode.value + " ");
}
}

public void traverse(int traverseType) {
switch (traverseType) {
case 1:
preOrder(root);
break;
case 2:
inOrder(root);
break;
case 3:
postOrder(root);
break;
default:
break;
}
// 依據樹節點的值刪除樹中的一個節點
public boolean delete(int value) {
// 遍歷樹過程中的當前節點
Node current = root;
// 要刪除節點的父節點
Node parent = root;
// 記錄樹的節點為左孩子節點或右孩子節點
boolean isLeftChild = true;
while (current.value != value) {
parent = current;
// 要刪除的節點在當前節點的左子樹里
if (value < current.value) {
isLeftChild = true;
current = current.leftChild;
}
// 要刪除的節點在當前節點的右子樹里
else {
isLeftChild = false;
current = current.rightChild;
}
// 在樹中沒有找到要刪除的節點
if (current == null)
return false;
}
// 要刪除的節點為葉子節點
if (current.leftChild == null && current.rightChild == null) {
// 要刪除的節點為根節點
if (current == root)
root = null;
// 要刪除的節點為左孩子節點
else if (isLeftChild)
parent.leftChild = null;
// 要刪除的節點為右孩子節點
else
parent.rightChild = null;
}
// 要刪除的節點有左孩子節點,沒有右孩子節點
else if (current.rightChild == null) {
// 要刪除的節點為根節點
if (current == null)
root = current.leftChild;
// 要刪除的節點為左孩子節點
else if (isLeftChild)
parent.leftChild = current.leftChild;
// 要刪除的節點為右孩子節點
else
parent.rightChild = current.leftChild;
}
// 要刪除的節點沒有左孩子節點,有右孩子節點
else if (current.leftChild == null) {
// 要刪除的節點為根節點
if (current == root)
root = root.rightChild;
// 要刪除的節點為左孩子節點
else if (isLeftChild)
parent.leftChild = current.rightChild;
// 要刪除的節點為右孩子節點
else
parent.rightChild = current.rightChild;
}
// 要刪除的接節點既有左孩子節點又有右孩子節點
else {
Node successor = getSuccessor(current);
// 要刪除的節點為根節點
if (current == root)
root = successor;
// 要刪除的節點為左孩子節點
else if (isLeftChild)
parent.leftChild = successor;
// 要刪除的節點為右孩子節點
else
parent.rightChild = successor;
}
return true;
}

// 找到要刪除節點的替補節點
private Node getSuccessor(Node delNode) {
// 替補節點的父節點
Node successorParent = delNode;
// 刪除節點的替補節點
Node successor = delNode;
Node current = delNode.rightChild;
while (current != null) {
// successorParent指向當前節點的上一個節點
successorParent = successor;
// successor變為當前節點
successor = current;
current = current.leftChild;
}
// 替補節點的右孩子節點不為空
if (successor != delNode.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}
}

public class TreeApp {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(8);
tree.insert(50);
tree.insert(45);
tree.insert(21);
tree.insert(32);
tree.insert(18);
tree.insert(37);
tree.insert(64);
tree.insert(88);
tree.insert(5);
tree.insert(4);
tree.insert(7);

System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();

System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();

System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();

System.out.println(tree.delete(7));

System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();

System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();

System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();

}
}

❽ java實現tree樹性能如何

樹與二叉樹實現差不多,二叉樹類變數裡面有兩個節點,通過配置一些參數讓資料庫性能達到最優。
用Java實現的數據樹形封裝。

❾ java tree目錄

private DefaultMutableTreeNode root, red, green, blue;
private JTree jtree1;
private JPanel jpanel1;

private JFrame frame;

public yimin() {
// TODO Auto-generated constructor stub
super();
init();
}

public void init() {
frame = new JFrame("jtree");
root = new DefaultMutableTreeNode("Color");
red = new DefaultMutableTreeNode("red");
green = new DefaultMutableTreeNode("green");
blue = new DefaultMutableTreeNode("blue");

root.add(red);
red.add(blue);
root.add(green);

jtree1 = new JTree(root);
jpanel1 = new JPanel();
JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jtree1, jpanel1);
jsplitpane.setOneTouchExpandable(true);
jsplitpane.setMinimumSize(new Dimension(100, 50));
frame.getContentPane().add(jsplitpane);
frame.setSize(600, 500);
frame.setLocation(50, 50);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}



採納吧,給分吧、

熱點內容
c語言稀疏矩陣轉置矩陣 發布:2025-02-01 03:47:57 瀏覽:530
坦克世界掛機腳本有哪些 發布:2025-02-01 03:07:41 瀏覽:133
串口編程at 發布:2025-02-01 03:06:05 瀏覽:908
合資汽車配置有什麼 發布:2025-02-01 02:56:07 瀏覽:78
wifi共享精靈源碼 發布:2025-02-01 02:40:15 瀏覽:973
java軟體怎麼安裝 發布:2025-02-01 02:40:09 瀏覽:549
河北稅務局電子密碼是什麼 發布:2025-02-01 02:40:07 瀏覽:835
檢查伺服器設置是什麼意思 發布:2025-02-01 02:31:26 瀏覽:185
神偷四第四章密碼是多少 發布:2025-02-01 02:07:29 瀏覽:13
qq登錄在哪個文件夾 發布:2025-02-01 01:57:59 瀏覽:627