gpt4 book ai didi

java - 使用JTree获取Java中每个子类和兄弟类的名称

转载 作者:行者123 更新时间:2023-12-01 10:17:10 28 4
gpt4 key购买 nike

我创建了 JavaTree。现在我想获取每个类的所有子类和兄弟类名称。我可以使用 selectedNode.getParent().toString() 获取父类(super class)。但是子类的以下代码给出了类似 java.util.Vector$1@3ae19358 的结果,而对于同级类,我找不到方法。有没有办法获取每个子类和兄弟类的名称?

    import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample extends JFrame
{

/****************** Developing a Simple JTree************************/
private JTree tree;
private JLabel selectedLabel; //for Event Handlers

public TreeExample()
{
//create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("AA");
//create the child nodes
DefaultMutableTreeNode bb = new DefaultMutableTreeNode("BB");
DefaultMutableTreeNode cc = new DefaultMutableTreeNode("CC");

//add the child nodes to the root node
root.add(bb);
root.add(cc);

//create the tree by passing in the root node
tree = new JTree(root);
add(tree);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree Example");
this.pack();
this.setVisible(true);


/****************** Adding More Children************************/
videoInfoNode.add(new DefaultMutableTreeNode("DD"));

foodInfoNode.add(new DefaultMutableTreeNode("EE"));
foodInfoNode.add(new DefaultMutableTreeNode("FF"));
foodInfoNode.add(new DefaultMutableTreeNode("GG"));

//To get the selected node information
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

//sub class
if(selectedNode.getAllowsChildren()){----??????????
selectedLabel.setText(selectedNode.children().toString());}-----?????????
else{
System.out.println("no children");
}

//sibling class
System.out.println(selectedNode.getSibling().....);--------????????
}
});

} //end

//main
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TreeExample();
}
});
}
}

最佳答案

I could take super class using selectedNode.getParent().toString().

我猜你的意思是TreeNode

if(selectedNode.getAllowsChildren()){----??????????

也许您需要使用TreeNode#isLeaf()而不是DefaultMutableTreeNode#getAllowsChildren() :

import java.awt.BorderLayout;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class TreeExample2 extends JFrame {
/****************** Developing a Simple JTree************************/
private JTree tree;
//private JLabel selectedLabel; //for Event Handlers

public TreeExample2() {
//create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("AA");
//create the child nodes
DefaultMutableTreeNode bb = new DefaultMutableTreeNode("BB");
DefaultMutableTreeNode cc = new DefaultMutableTreeNode("CC");

//add the child nodes to the root node
root.add(bb);
root.add(cc);

//create the tree by passing in the root node
tree = new JTree(root);
add(tree);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree Example");
this.pack();
this.setVisible(true);


/****************** Adding More Children************************/
bb.add(new DefaultMutableTreeNode("DD"));

cc.add(new DefaultMutableTreeNode("EE"));
cc.add(new DefaultMutableTreeNode("FF"));
cc.add(new DefaultMutableTreeNode("GG"));

//To get the selected node information
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override public void valueChanged(TreeSelectionEvent e) {
Object o = tree.getLastSelectedPathComponent();
if (o instanceof DefaultMutableTreeNode) {
//TreeNode selectedNode = (TreeNode) o;
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) o;
System.out.println("----");

//children
if (!selectedNode.isLeaf() && selectedNode.getChildCount() > 0) {
Enumeration enumeration = selectedNode.children();
while (enumeration.hasMoreElements()) {
TreeNode node = (TreeNode) enumeration.nextElement();
System.out.println("child: " + node);
}
} else {
System.out.println("no children");
}

//sibling
TreeNode parentNode = selectedNode.getParent();
if (parentNode != null) {
for (int i = 0; i < parentNode.getChildCount(); i++) {
TreeNode node = parentNode.getChildAt(i);
if (!selectedNode.equals(node)) {
System.out.println("sibling: " + node);
}
}
}
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
new TreeExample2();
}
});
}
}

关于java - 使用JTree获取Java中每个子类和兄弟类的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com