2012-01-06 5 views

答えて

2

hereを示すように、あなたはINSTANCEOFキーワードgetComponentsと使用を使用することができます。

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

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

それらが構築されているときにコレクションに追加、またはあなたのUIのすべてのコンポーネントを得るためにあなたのトップレベルのコンテナにgetComponentsを使用しています。その後、便宜のために、タイプ別にコントロールのコレクションに参照を戻す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 

を繰り返し、以下のリストのようなものがあるかもしれません。

関連する問題