简体   繁体   English

在Netbeans Swing GUI Builder中将组件添加到JFrame

[英]Adding components to JFrame in Netbeans Swing GUI Builder

I am trying to create my application using Netbeans GUI Builder, but I have a situation here. 我正在尝试使用Netbeans GUI Builder创建我的应用程序,但是这里有一种情况。

When I drag and drop a component(Jlabel or any other used defined component) to JPanel from Palette window of GUI Builder, the Java code is automatically added by the Netbeans. 当我从GUI Builder的Palette窗口中将组件(Jlabel或任何其他使用的已定义组件)拖放到JPanel时,Netbeans会自动添加Java代码。 For eg. 例如。 the following code is generated: 生成以下代码:

**private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

}**

Now I have a ArrayList which need to store the component objct which is added by GUIBuilder. 现在,我有了一个ArrayList,它需要存储GUIBuilder添加的组件objct。 In this case the object added is jLabel1. 在这种情况下,添加的对象是jLabel1。

ArrayList updateComponentsList = new ArrayList();

So I need to store this object in this ArrayList. 所以我需要将此对象存储在此ArrayList中。 Infact I need the new component object to be automatically added to this list, whenever a new component is added by GUIBuilder. 实际上,每当GUIBuilder添加新组件时,我都需要将新组件对象自动添加到此列表中。

Since GUIBuilder generates the Java code for added component automatically, how do I make GUIBuilder to update this ArrayList automatically whenever a new component is added? 由于GUIBuilder自动为添加的组件生成Java代码,因此,每当添加新组件时,如何使GUIBuilder自动更新此ArrayList?

Can anybody please help me to figure this out? 有人可以帮我解决这个问题吗?

Thanks in advance. 提前致谢。

It might work for you. 它可能为您工作。 When new components are added in your current GUI, it automatically calls the initComponent() method to redraw the JFrame and you can get the updated list of component by calling this below method at the end of initComponent() block. 当在当前GUI中添加新组件时,它将自动调用initComponent()方法以重绘JFrame,并且可以通过在initComponent()块末尾调用以下方法来获取组件的更新列表。

public static List getAllComponents(final Container c) { 公共静态列表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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM