Here you can find the source of checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow)
Parameter | Description |
---|---|
_rootComponent | the root component |
_print | whether to print the result or not |
_currentColumn | the current column number |
_currentRow | the current row number |
private static Rectangle checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow)
//package com.java2s; /*/*from ww w .java 2 s .c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.awt.Component; import java.awt.Panel; import java.awt.Rectangle; import java.awt.Window; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { /** * Check the components. * Recursive method which returns the total amount of rows and columns. * * @param _rootComponent the root component * @param _print whether to print the result or not * @param _currentColumn the current column number * @param _currentRow the current row number * @return the total amount of rows and columns of the * current graphical-user-interface-configuration */ private static Rectangle checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow) { //save given values. int depth = _currentColumn; int amount = _currentRow; int maxDepth = _currentColumn; if (_rootComponent.getWidth() <= 0 || _rootComponent.getHeight() <= 0) { System.err.println(_print + _rootComponent.getClass().getSimpleName() + _rootComponent.getSize()); } else { System.out.println(_print + _rootComponent.getClass().getSimpleName() // + _c.getSize() ); } if (_rootComponent instanceof JPanel) { for (Component x : ((JPanel) _rootComponent).getComponents()) { Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1); //update max depth and amount amount += pnt_recursiv.y; maxDepth = Math.max(maxDepth, pnt_recursiv.width); } } else if (_rootComponent instanceof JFrame) { for (Component x : ((JFrame) _rootComponent).getContentPane().getComponents()) { Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1); //update max depth amount += pnt_recursiv.y; maxDepth = Math.max(maxDepth, pnt_recursiv.width); } } else if (_rootComponent instanceof Panel) { for (Component x : ((Panel) _rootComponent).getComponents()) { Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1); //update max depth amount += pnt_recursiv.y; maxDepth = Math.max(maxDepth, pnt_recursiv.width); } } else if (_rootComponent instanceof Window) { for (Component x : ((Window) _rootComponent).getComponents()) { Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1); //update max depth amount += pnt_recursiv.y; maxDepth = Math.max(maxDepth, pnt_recursiv.width); } } Rectangle pnt_res = new Rectangle(depth, amount, maxDepth, 0); return pnt_res; } }