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

时间:2023-01-28 22:40:01

I have designed a page that consists of some panels, labels and 60 buttons. I designed them using swing components by drag & drop.

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

Now I need to put all Jbuttons to arraylist how can I do that using a loop?

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

1 个解决方案

#1


3  

This is example of finding buttons on the panel - you can try to adopt it to your case (from this post)

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

    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
                }
            }
        }
    }

#1


3  

This is example of finding buttons on the panel - you can try to adopt it to your case (from this post)

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

    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
                }
            }
        }
    }