2012-01-06 32 views

回答

2

您可以使用getComponents和使用instanceof關鍵字如圖所示here

Component[] components = this.getComponents(); 
List<Component> buttons = new ArrayList<Component>(); 

for (Component component : components) 
{ 
    if (component instanceof JButton) 
    { 
     buttons.add(component); 
    } 
} 
0

它們添加到收藏他們正在建造時,或使用getComponents你的頂層容器來獲取所有組件的UI。然後提供訪問方法,例如getAllButtons(),爲方便起見,它們將引用返回給類型控件集合。

根據您的需求,您可能有類似的方法

getAllButtons()  // returns an array, LinkedList, or other collection of all Buttons 
addButton(Button)  // adds a specific, new Button to the global list of Buttons 
removeButton(Button) // removes a specific, existing Button from the global list 
removeAllButtons()  // clears the global list of Buttons 

你需要支持的任何組件/控件類型重複以下列表。