2012-11-06 6 views
0

GridBagLayouts에 문제가 있습니다. Java - 중첩 GridBagLayout 레이아웃 관리자

enter image description here

내가 "센터"구성 요소 "오른쪽 상단"만약의 아래쪽 가장자리 위로 이동할 수 있도록하려면 아래 그림과 같이

나는 배치 몇 가지 구성 요소가 "상단"구성 요소가 충분히 작아집니다. 그러나 그리드의 다른 행에 있어야하므로 문제가 발생합니다.

이 문제를 해결하려면 - 전체 오른쪽 열을 고유 한 레이아웃 관리자로 자체 컨테이너로 정의합니다. 두 열이 독립적으로 동작 할 수 있기를 기대하지만, 같은 문제가 발생합니다! 두 레이아웃 관리자가 어떻게 아직도 상호 작용할 수 있는지는 알 수 없습니다. 아무도 내 문제가 무엇인지 설명 할 수 있습니까? 여기

는 (내 클래스 확장 애플릿에) 관련 코드 :

public void init(){ 
    addComponents(); 
} 

private void addComponents(){ 
    setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    Left left = new Left(); 
    Top top = new Top(); 
    Center center = new Center(); 
    TopRight topRight = new TopRight(); 
    BottomRight bottomRight = new BottomRight(); 
    Bottom bottom = new Bottom(); 

    topRight.setPreferredSize(new Dimension(200,200)); 
    top.setPreferredSize(new Dimension(0,200)); 
    center.setPreferredSize(new Dimension(0,100)); 
    container.setPreferredSize(new Dimension(200,200)); 

    c.fill = GridBagConstraints.BOTH; 

    c.gridx = 0; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 3; 
    c.weightx = 0.2; c.weighty = 1.0; 
    this.add(left,c); 

    c.gridx = 1; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.8; 
    this.add(top,c); 

    c.gridx = 1; c.gridy = 1; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.0; 
    this.add(center,c); 

    c.gridx = 1; c.gridy = 2; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.2; 
    this.add(bottom,c); 

    JPanel container = new JPanel(); 
    container.setLayout(new GridBagLayout()); 

    c.gridx = 2; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 3; 
    c.weightx = 0.0; c.weighty = 1.0; 
    this.add(container,c); 

    GridBagConstraints c2 = new GridBagConstraints(); 
    c2.fill = GridBagConstraints.BOTH; 

    c2.gridx = 0; c2.gridy = 0; 
    c2.gridwidth = 1; c2.gridheight = 1; 
    c2.weightx = 0.0; c2.weighty = 0.0; 
    container.add(topRight,c2); 

    c2.gridx = 0; c2.gridy = 1; 
    c2.gridwidth = 1; c2.gridheight = 1; 
    c2.weightx = 0.0; c2.weighty = 1.0; 
    container.add(bottomRight,c2); 
} 

참고 : (왼쪽, 위, 등) 표시된 각각의 구성 요소가 JPanel의의 확장 - 자사의 페인트 각 (그래픽 g) 메소드를 재정 의하여 색상을 채 웁니다.

미리 감사하고, 긴 읽기 죄송합니다,

조나단

+0

경계가 교차 할 때 어떤 일이 발생하는지 명확히 알기 위해 오른쪽 상단 구성 요소가 갑자기 크기를 전환하여 (가운데 바람직한 크기보다 훨씬 작음) 센터 구성 요소가 위쪽으로 계속 이동할 수 있습니다. 중앙 구성 요소가 오른쪽 상단의 기본 높이를지나 위로 이동하기 전까지는 새로운 크기로 유지됩니다. – Porthos3

답변

0

나는 문제를 해결. 그것은 Top 구성 요소의 선호하는 높이를 불필요하게 설정하는 것과 관련이 있습니다. 내가 원하는 선과 같이 작동 한 선을 제거했을 때.