无法将JTree添加到JInternalFrame的JPanel

时间:2014-10-28 05:46:38

标签: java swing jpanel jtree

我的JInternalframe中有两个面板。我想将Jtree添加到名为topPanel的第二个Jpanel。 但是我无法将Jtree添加到topPanel。请帮助我。这是我的代码:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Deck");

DefaultMutableTreeNode itemClubs = new DefaultMutableTreeNode("Clubs");
addAllCard(itemClubs);
root.add(itemClubs);

DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode("Diamonds");
addAllCard(itemDiamonds);
root.add(itemDiamonds);

DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode("Spades");
addAllCard(itemSpades);
root.add(itemSpades);

DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode("Hearts");
addAllCard(itemHearts);
root.add(itemHearts);
DefaultTreeModel treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
scrollPane = new JScrollPane(tree);
// scrollPane.setViewportView(tree);

scrollPane.getViewport().add(tree);

topPanel.add(scrollPane, BorderLayout.CENTER);

topPanel.setVisible(true);  

tree.setVisible(true);

1 个答案:

答案 0 :(得分:1)

基本上,对我来说,必须有一些你没有向我们展示的东西。

考虑提供展示您问题的runnable example。这将减少混淆和更好的响应

TreeExample

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TreeTest {

    public static void main(String[] args) {
        new TreeTest();
    }

    public TreeTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JDesktopPane desktopPane = new JDesktopPane();
                JInternalFrame inFrame = new JInternalFrame("Tree", true, true, true, true);
                inFrame.add(new TestPane());
                inFrame.pack();
                inFrame.setVisible(true);
                desktopPane.add(inFrame);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(desktopPane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Deck");
            DefaultMutableTreeNode itemClubs = new DefaultMutableTreeNode("Clubs");
            root.add(itemClubs);
            DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode("Diamonds");
            root.add(itemDiamonds);
            DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode("Spades");
            root.add(itemSpades);
            DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode("Hearts");
            root.add(itemHearts);
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            JTree tree = new JTree(treeModel);
//            tree.setRootVisible(true);
//            tree.setShowsRootHandles(true);

            JPanel topPanel = new JPanel(new BorderLayout());
            topPanel.add(new JScrollPane(tree));
            add(topPanel);
        }

    }

}