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

后端 未结 2 863
失恋的感觉
失恋的感觉 2021-01-17 18:38

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

Is there any method like foreach in C# to deal all the child components of a JPanel?

2条回答
  •  攒了一身酷
    2021-01-17 19:25

    if you have more than one JPanel and you want to get all components Name try this:

    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("=====================");
        }
    }
    

提交回复
热议问题