JAVA SWING - 设计一个像自定义结构的树

时间:2017-06-11 16:49:20

标签: java swing

Mock-up image

大家好,我正在尝试使用swing开发一个类似树的结构。

主要部分称为元素。每个元素都有2个部分叫做MainView和ChildrenView。

MainView包含自定义组件(此视图的组件将由Element的子类添加)。 MainView的高度严格固定为50px。它的宽度是动态的,并延伸到最后。

ChildrenView将子元素视为列表。 ChildrenView有一个左固定偏移量,并向右开始。

我在设置尺寸时遇到问题。元素的高度不固定,scrollablePane不可滚动。

提前致谢。

public class DesktopEditor {
    private static final int mainViewHeight = 50;
    static final int padding = 5;
    static final int childOffset = 30;

    public static void main(final String[] arg) {
        openEditor();
    }

    public static void openEditor() {
        JFrame frame = new JFrame("JFrame Example 1");

        ChildrenView mainPanel = new ChildrenView();

        mainPanel.addChild(new Text().addChild(new Text()));
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());
        mainPanel.addChild(new Text());


        JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        frame.getContentPane().add(scrollPane);


        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }

    /**
     * here is a example of subClasses of Element
     */
    public static class Text extends Element {
        public Text () {
            super();
            mainView.add(new JTextArea("hello"));
            mainView.add(new Button("hello2"));
        }
    }

    public static class Element extends JPanel {
        SpringLayout layout;

        MainView mainView;
        ChildrenView childView;

        public Element() {
            super();
            setLayout(layout = new SpringLayout());
            // child and mainView
            this.mainView = new MainView();
            this.childView = new ChildrenView();
            add(mainView);
            add(childView);
            // adjust layout
            layout.putConstraint(SpringLayout.WEST, mainView, padding, SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.EAST, mainView, padding, SpringLayout.EAST, this);
            layout.putConstraint(SpringLayout.NORTH, mainView, padding, SpringLayout.NORTH, this);
            layout.putConstraint(SpringLayout.SOUTH, mainView, padding + mainViewHeight, SpringLayout.NORTH, this);
            //
            layout.putConstraint(SpringLayout.WEST, childView, padding + childOffset, SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.EAST, childView, padding, SpringLayout.EAST, this);
            layout.putConstraint(SpringLayout.NORTH, childView, padding, SpringLayout.SOUTH, mainView);
            layout.putConstraint(SpringLayout.SOUTH, childView, padding, SpringLayout.SOUTH, this);

            setMinimumSize(new Dimension(1, mainView.getHeight() + childView.getHeight() + padding *2));
        }

        public Element addChild(Element element) {
            childView.addChild(element);

            setMinimumSize(new Dimension(1, mainView.getHeight() + childView.getHeight() + padding *2));
            layout.minimumLayoutSize(this);

            invalidate();
            repaint();

            return this;
        }
    }

    public static class ChildrenView extends JPanel {
        public ChildrenView() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            setBackground(Color.RED);

            setPreferredSize(new Dimension(1,0));
        }

        public int height() {
            int height = 0;
            for (Component c: getComponents())
                height += c.getHeight();
            return height;
        }

        public Element addChild(Element element) {
            add(element);

            setMinimumSize(new Dimension(1, height()));
            invalidate();
            repaint();

            return element;
        }
    }

    public static class MainView extends JPanel {

        public MainView() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            setBackground(Color.BLUE);
            setMinimumSize(new Dimension(0, mainViewHeight));
            setMaximumSize(new Dimension(Integer.MAX_VALUE, mainViewHeight));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您询问如何使用Swing制作树,请参阅下面的代码

package net.codejava.swing;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample extends JFrame
{
    private JTree tree;
    public TreeExample()
    {
        //create the root node
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        //create the child nodes
        DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables");
        DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("Fruits");
        //add the child nodes to the root node
        root.add(vegetableNode);
        root.add(fruitNode);

        //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);
    }

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