问题
How to count number of controls like JTextField, J Label and so on so..in java swing form designing,for example if we use only one textbox and one text field means i need the output as follows count value is 2.how to do this?
回答1:
The container for the controls contains a list of controls, see Container.getComponents()
. Come to think, it also has a method for the count of the controls, if that's all you want, Container.getComponentCount().
If you want this for all the controls contained within containers contained (ultimately) in a frame, you will need to look at each one recursively; start with the frame, and for each component that is a container, get the controls/count in that container, etc. Keep in mind that panels can have panels.
rc
回答2:
As they should all be inside your Container
object you can use the getComponentCount()
method to return you an int
of the number of components.
Container c;
int count;
count = c.getComponentCount();
回答3:
With SwingX, you have in class org.jdesktop.swingx.util.WindowUtils public static List<Component> getAllComponents(Container c)
. With that you have all components and subcomponents in container.
来源:https://stackoverflow.com/questions/8556982/java-swing-to-count-all-controls