如何获得JSplitPane的特定组件



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

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

是否有办法从对象splitPane中获得panel1panel2 ?

你可以得到一个数组的所有组件在一个容器(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());
    }

相关内容

  • 没有找到相关文章

最新更新