簡體   English   中英

JAVA SWING-設計類似自定義結構的樹

[英]JAVA SWING - Designing a tree like custom structure

模擬圖像

大家好,我正在嘗試使用秋千開發出類似樹的結構。

主要部分稱為元素。 每個元素都有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));
        }
    }
}

如果您問如何使用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();
            }
        });
    }       
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM