如何获得JSplitPane的特定组件?

时间:2015-10-05 14:30:26

标签: java swing jsplitpane

想法是获取JSplitPane的特定组件,例如:

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);

有没有办法从对象panel1获取panel2splitPane

1 个答案:

答案 0 :(得分:1)

您可以获取容器内所有组件的数组(JSplitPane是一个容器):

    // 2 JPanels for example
    JPanel panel1 = new JPanel();
    panel1.setName("pan1");
    getContentPane().add(panel1);
    JPanel panel2 = new JPanel();
    panel2.setName("pan2");
    getContentPane().add(panel2);

    // creating the JSplitPane container with those panels
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);

    // get all components from the JSplitPane and print their names
    Component[] components=splitPane.getComponents();
    for(Component c:components){
        System.out.println(c.getName());
    }