java swing来计算所有控件的数量
如何在java swing表单设计中计算JTextField、J Label等控件的数量,例如,如果我们只使用一个文本框和一个文本字段,则意味着我需要如下输出计数值为2。如何做这?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控件的容器包含控件列表,请参阅
Container.getComponents()
。想想看,它还有一个用于控件计数的方法,如果这就是您想要的,Container.getComponentCount()。
如果您希望对容器中包含的所有控件进行此操作(最终)在一个框架中,您需要递归地查看每个框架;从框架开始,对于作为容器的每个组件,获取该容器中的控件/计数等。请记住,面板可以有面板。
RC
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
由于它们都应该位于您的
Container
对象内,因此您可以使用getComponentCount()
方法返回组件数量的int
。As they should all be inside your
Container
object you can use thegetComponentCount()
method to return you anint
of the number of components.使用 SwingX,您可以在 org.jdesktop.swingx.util.WindowUtils 类中
public static List; getAllComponents(Container c)
。这样,容器中就有了所有组件和子组件。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.