当前位置:首页 » 编程语言 » 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);
}



采纳吧,给分吧、

热点内容
qq登录在哪个文件夹 发布:2025-02-01 01:57:59 浏览:624
如何加入安卓代理 发布:2025-02-01 01:51:40 浏览:2
我的世界手游服务器刷钻石教程 发布:2025-02-01 01:48:13 浏览:773
sqlifthen男女 发布:2025-02-01 01:44:59 浏览:690
幻灵和安卓哪个互通 发布:2025-02-01 01:43:33 浏览:648
电脑配置够但为什么打lol掉帧 发布:2025-02-01 01:37:08 浏览:316
21款朗逸哪个配置比较划算 发布:2025-02-01 01:35:32 浏览:976
建筑动画片脚本 发布:2025-02-01 01:35:21 浏览:469
管家婆如何用阿里云服务器 发布:2025-02-01 01:29:09 浏览:649
解压耳放 发布:2025-02-01 01:20:18 浏览:176