简体   繁体   English

如何获取Java Swing中一块面板的所有元件?

[英]How can I get all the components of a panel in Java Swing?

How can I get all the components of a panel in Java Swing?如何获取Java Swing中一块面板的所有元件?

Is there any method like foreach in C# to deal all the child components of a JPanel?有没有像C#中的foreach这样的方法来处理JPanel的所有子组件?

您可以使用getComponents方法:

Component[] components = jpanel.getComponents();

if you have more than one JPanel and you want to get all components Name try this:如果您有多个 JPanel 并且想要获取所有组件名称,请尝试以下操作:

public void getAllComponetsNameInJPanels(JPanel... panels) {
    Component[] components;
    String componentName;
    for (JPanel panel : panels) {
        components = panel.getComponents();            
        for (Component compo : components) {
            componentName = compo.getClass().getName();
            System.out.println(compo.getClass().getName().substring(componentName.indexOf("swing.") + "swing.".length(), componentName.length()));
        }
        System.out.println("=====================");
    }
}

getComponents() method will return all child components of a given components as an array. getComponents()方法将返回给定组件的所有子组件作为数组。

If you want to get all components including child components of child components you can write a recursive function and consolidate everything into one List.如果你想获取所有组件,包括子组件的子组件,你可以写一个递归的 function 并将所有内容合并到一个列表中。

Recursion, in programming, is a technique of solving problems by creating a method that calls itself until the desired result is achieved.递归,在编程中,是一种解决问题的技术,它通过创建一种方法来调用自身,直到获得所需的结果。

Below is an example that will allow you to get a component by name, regardless how deep it is within the structure :下面是一个示例,它允许您通过名称获取组件,无论它在结构中有多深

// Container is imported from java.awt
public static Container getChildComponentByName(Container parentComponent, String childName) {

    // Define data structures that will hold components
    Map<String, Container> allComponentsMap = new HashMap();
    List<Container> allComponents = new ArrayList<>();

    // Iterating through the components structure and adding it to the List using our recursive function
    addAllChildComponentsToList(allComponents, parentComponent);

    // Iterating through the List and adding them to a HashMap keyed with their name
    for (Container c : allComponents) {
        allComponentsMap.put(c.getName(), c);
    }

    // Returning a component with the given name
    if (allComponentsMap.containsKey(childName)) {
        return allComponentsMap.get(childName);
    } else {
        System.out.println("ERROR: No match found when looking for GUI child components.");
        return null;
    }
}

private static void addAllChildComponentsToList(List<Container> componentArr, Container parentComponent) {

    // Making a list with all child components
    List<Container> childComponentsArr = Arrays.stream(parentComponent.getComponents()).map(c -> (Container) c).collect(Collectors.toList());

    if (childComponentsArr.size() > 0) {

        for (Container c : childComponentsArr) {

            // Adding a child component to the passed List
            componentArr.add(c);

            // Repeating the process if child has its own child components
            if (c.getComponents().length > 0) {
                addAllChildComponentsToList(componentArr, c);
            }
        }
    } else {
        return;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM