如何使用 JTree Listener 动态更改带有 CardLayout 的 JPanel?

How to dynamically change a JPanel with a CardLayout using a JTree Listener?

我正在尝试使用 JTree 的内容更改带有 CardLayout 的面板。它仅适用于最后的选择。我应该对我的代码进行哪些侦听器或更改?

我也在控制台中写入文本,它似乎得到了正确的值。这就是让我感到沮丧的原因。

我的代码应该在单击一级节点时显示一级,在单击二级节点时显示二级。我用了

DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();

它只影响最后一个组件。我该如何解决?谢谢!

源代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class ProblemTree2 extends JFrame {

    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    private DefaultTreeModel model = new DefaultTreeModel(root);
    private JTree tree = new JTree(model);
    private JPanel card;

    public ProblemTree2() {

        card = new JPanel(new CardLayout());
        card.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        JLabel label1 = new JLabel("1st level");
        JLabel label2 = new JLabel("2nd level");

        DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(
                "1st level: Child 1");
        n1.add(new DefaultMutableTreeNode("2nd level: Child l"));
        DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(
                "1st level: Child 2");
        n2.add(new DefaultMutableTreeNode("2nd level: Child 2"));
        DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(
                "1st level: Child 3");
        n3.add(new DefaultMutableTreeNode("2nd level: Child 3"));


        card.add(label1,"1st level: Child 1");
        card.add(label1,"1st level: Child 2");
        card.add(label1,"1st level: Child 3");

        card.add(label2,"2nd level: Child l");
        card.add(label2,"2nd level: Child 2");
        card.add(label2,"2nd level: Child 3");

        root.add(n1);
        root.add(n2);
        root.add(n3);

        tree.setEditable(true);
        tree.setSelectionRow(0);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);


        tree.getSelectionModel().addTreeSelectionListener(
                new TreeSelectionListener() {
                    @Override
                    public void valueChanged(TreeSelectionEvent e) {

                        final CardLayout cards = (CardLayout) card
                                .getLayout();
                        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();
                        System.out.println(selectedNode.toString());
                        cards.show(card,selectedNode.toString());
                    }
                });


        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(500,500));
        getContentPane().add(scrollPane, BorderLayout.WEST);
        getContentPane().add(card, BorderLayout.CENTER);

        setSize(1000, 600);
        setVisible(true);
    }

    public static void main(String[] arg) {
        ProblemTree2 pt = new ProblemTree2();
    }
}

这个...

card.add(label1, "1st level: Child 1");
card.add(label1, "1st level: Child 2");
card.add(label1, "1st level: Child 3");

card.add(label2, "2nd level: Child l");
card.add(label2, "2nd level: Child 2");
card.add(label2, "2nd level: Child 3");

导致你的问题,你不能为一个组件的同一个实例分配多个键。

当您第二次尝试添加该组件时,它首先会从其父容器中删除,这会导致 CardLayout 也删除 "name",这意味着只有行。 ..

card.add(label1, "1st level: Child 3");
// and...
card.add(label2, "2nd level: Child 3");

正在实际工作(已添加到 UI/CardLayout

您必须为每个名称提供组件的新实例。

或者,您可以为您的 TreeNode 提供更多详细信息,这将使您能够确定它是哪个级别并显示该级别的正确组件,这意味着您的 CardLayout, 每个级别一个。

DefaultMutableTreeNode 允许您为其提供 "user" Object。默认情况下,它使用此对象的 toString 方法作为节点的文本,但您可以使用 TreeCellRenderer 对其进行自定义或为 toString 方法提供一个值 "custom level"对象