2014-05-08 3 views
1

패널을 만들 때 GridBagLayout을 사용합니다.GridBagLayout aligment issue

패널의 오른쪽 구석에있는 삭제 아이콘을 사용하고 싶습니다. 그러나 JTextArea이 패널에 추가되지 않으면 삭제 아이콘이 왼쪽으로 이동합니다.

누군가 제대로 할 수있는 방법을 말해 줄 수 있습니까?

rendered

는에 weightxanchor의 조합을 사용하여 패널

private void buildTopJPane() { 

    try { 


     c.gridx = 0; 
     c.gridy = 0; 
     c.fill = GridBagConstraints.NONE; 
     this.add(setAttonIconCreator(), c); 

     c.gridx = 5; 
     c.gridy = 0; 
     c.gridwidth = 4; 
     this.add(setDeleteIcon(), c); 

     c.gridx = 0; 
     c.gridy = 1; 
     c.gridwidth = 4; 
     this.add(setPageDateLabel(), c); 

     if (thisComment.content != null) { 
      if (thisComment.content.length() > 0) 
       c.gridx = 0; 
      c.gridy = 2; 
      c.gridwidth = 4; 
      this.add(setContent(), c); 
     } 
    } catch (Exception err) { 
     Utility.DisplayErrorMsg(pageErrorPrefix + err.getMessage().toString()); 
    } 
} 

답변

4

에 구성 요소를 추가하는 코드가 있습니다

public void setting() { 
    try { 

     this.setLayout(new GridBagLayout()); 
     c = new GridBagConstraints(); 
     c.anchor = GridBagConstraints.WEST; 
     c.fill = GridBagConstraints.BOTH; 
     c.weightx = 1.0; // **** comment this line out to see effect **** 
     c.weighty = 1.0; // **** comment this line out to see effect **** 
     this.setBorder(BorderFactory.createLineBorder(Color.black)); 
     addMouseListener(this); 
     buildTopJPane(); 
     setVisible(true); 
    } catch (Exception err) { 
     Utility.DisplayErrorMsg(pageErrorPrefix + err.getMessage().toString()); 
    } 
} 

을 설정 GridBagLayout에 패널 내 코드가있다 삭제 아이콘.

예를 들어 weightx = 1anchor = GridBagConstraints.WEST을 사용해보십시오. 여기에서 다른 콘텐츠도 왼쪽으로 이동합니다.

댓글 패널에서 weightx = 1을 사용해 볼 수도 있습니다.

또한 gridwidth을 더 잘 사용할 수 있으며 셀 배치를 사용하면 더 좋은 결과를 얻을 수있는 구성 요소를 겹치게 할 수 있습니다.

다음 구성 요소에서 사용하기 전에 제약 조건을 재설정하는 것을 잊지 마십시오.

+0

제안 해 주셔서 감사합니다. 삭제 아이콘에 GridBagConstraints.NORTHEAST를 설정하고 오른쪽 상단 구석에 배치합니다. – user819774