-1

我设计了一个由一些面板、标签和 60 个按钮组成的页面。我通过拖放使用摆动组件设计了它们。

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

4

1 回答 1

3

这是在面板上查找按钮的示例 - 您可以尝试将其应用于您的案例(来自这篇文章)

    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
                }
            }
        }
    }
于 2013-04-04T13:16:39.047 回答