如何将JFrame中的Jbuttons添加到arraylist?

时间:2013-04-04 13:09:44

标签: java swing

我设计了一个由一些面板,标签和60个按钮组成的页面。我使用摆动组件设计了它们。下降。

现在我需要把所有Jbuttons都放到arraylist中我怎么能用循环呢?

1 个答案:

答案 0 :(得分: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
                }
            }
        }
    }