我设计了一个由一些面板,标签和60个按钮组成的页面。我通过拖动&下拉来使用摆动组件设计它们。如何添加JFrame中的Jbutton到arraylist?
现在我需要把所有的Jbuttons放到arraylist中如何使用循环来做到这一点?
我设计了一个由一些面板,标签和60个按钮组成的页面。我通过拖动&下拉来使用摆动组件设计它们。如何添加JFrame中的Jbutton到arraylist?
现在我需要把所有的Jbuttons放到arraylist中如何使用循环来做到这一点?
这是例如发现面板上的按钮 - 你可以尝试将其采纳你的情况下(从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
}
}
}
}
这取决于你的代码。由于我们看不到您的代码,因此我们无法回答您的问题。我只能说,你必须在列表中调用'add()'方法。 – jlordo 2013-04-04 13:11:32
请张贴您现有的代码。 – Robert 2013-04-04 13:28:48