2013-04-04 61 views
-1

我设计了一个由一些面板,标签和60个按钮组成的页面。我通过拖动&下拉来使用摆动组件设计它们。如何添加JFrame中的Jbutton到arraylist?

现在我需要把所有的Jbuttons放到arraylist中如何使用循环来做到这一点?

+2

这取决于你的代码。由于我们看不到您的代码,因此我们无法回答您的问题。我只能说,你必须在列表中调用'add()'方法。 – jlordo 2013-04-04 13:11:32

+0

请张贴您现有的代码。 – Robert 2013-04-04 13:28:48

回答

3

这是例如发现面板上的按钮 - 你可以尝试将其采纳你的情况下(从this后)

Component[] components = aPanel.getComponents(); 
    if(components != null) 
    { 
     int numComponents = components.length; 
     for(int i = 0; i < numComponents; i++) 
     { 
      Component c = components[i]; 
      if(c != null) 
      { 
       if(c instanceof JButton) 
       { 
        // Add button to your list 
       } 
      } 
     } 
    } 
+0

非常感谢你! – RSF 2013-04-04 13:36:02

+0

不客气。下一次,当你在这里提出你的问题,提供更多的细节 - 它很难得到没有代码和其他细节的建议 – evgenyl 2013-04-04 13:44:10

+0

因为我的代码太长我无法发布它,对此感到抱歉.. – RSF 2013-04-04 14:04:06