2010-05-02 5 views
1

나는 자바에서이 BoxLayout 레이아웃 매니저를 사용하고, 및 구성 요소의 무리를 정렬 한 정렬 :모든 패널 구성 요소 자바

myLabel.setAlignmentX(Component.LEFT_ALIGNMENT); 
myTextBox.setAlignmentX(Component.LEFT_ALIGNMENT); 
myButton.setAlignmentX(Component.LEFT_ALIGNMENT); 
... 

을 나는 요소를 많이 가지고 있고,이 맨 위에 보인다. 속기 방법이 있습니까?

다음을 시도했지만 setAlignmentX는 Component 내부의 메소드가 아닙니다.

for (Component c : personPanel.getComponents()) { 
    c.setAlignmentX(Component.LEFT_ALIGNMENT); 
} 

답변

3

setAlignmentXJComponent으로 정의됩니다.

당신은 확인 후 캐스팅 수 : 당신이 당신의 구성 요소를 중첩 한 경우

for (Component c : personPanel.getComponents()) { 
    if(c instanceof JComponent) { 
     ((JComponent)c).setAlignmentX(Component.LEFT_ALIGNMENT); 
    } 
} 

는, 그 밖으로 재귀 적 방법을해야 할 수도 있습니다.