Java 如何获取 JFrame 中的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6495769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all elements inside a JFrame?
提问by Renato Dinhani
I have this code to get all the elements I need and do some processing. The problem is I need to specify every panel I have to get the elements inside it.
我有这个代码来获取我需要的所有元素并进行一些处理。问题是我需要指定每个面板,我必须在其中获取元素。
for (Component c : panCrawling.getComponents()) {
//processing
}
for (Component c : panFile.getComponents()) {
//processing
}
for (Component c : panThread.getComponents()) {
//processing
}
for (Component c : panLog.getComponents()) {
//processing
}
//continue to all panels
I want to do something like this and get all the elements without need specefy all the panels names. How I do this. The code below don't get all the elements.
我想做这样的事情并获得所有元素而无需指定所有面板名称。我如何做到这一点。下面的代码没有得到所有的元素。
for (Component c : this.getComponents()) {
//processing
}
采纳答案by aioobe
You can write a recursive method and recurse on every container:
您可以编写递归方法并在每个容器上递归:
This siteprovides some sample code:
该站点提供了一些示例代码:
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof Container)
compList.addAll(getAllComponents((Container) comp));
}
return compList;
}
If you only want the components of the immediate sub-components, you could limit the recursion depth to 2.
如果您只想要直接子组件的组件,则可以将递归深度限制为 2。
回答by toto2
回答by Vinzie Villamor
for (Component c : mainPanel.getComponents()) {
for (Component sc : ((JPanel) c).getComponents()) {
if (sc instanceof JTextField) {
//process
}
}
}
in my code, im just getting all instances of jtextfield. u can use same logic. this is just example of getting all sub-components from components you have taken. Hope it will help u out.
在我的代码中,我只是获取 jtextfield 的所有实例。你可以使用相同的逻辑。这只是从您采用的组件中获取所有子组件的示例。希望它会帮助你。
回答by flakes
If you want to find all components of a given type, then you can use this recursive method!
如果要查找给定类型的所有组件,则可以使用此递归方法!
public static <T extends JComponent> List<T> findComponents(
final Container container,
final Class<T> componentType
) {
return Stream.concat(
Arrays.stream(container.getComponents())
.filter(componentType::isInstance)
.map(componentType::cast),
Arrays.stream(container.getComponents())
.filter(Container.class::isInstance)
.map(Container.class::cast)
.flatMap(c -> findComponents(c, componentType).stream())
).collect(Collectors.toList());
}
and it can be used like this:
它可以像这样使用:
// list all components:
findComponents(container, JComponent.class).stream().forEach(System.out::println);
// list components that are buttons
findComponents(container, JButton.class).stream().forEach(System.out::println);