2013-04-18 2 views
1

GridBagLayout의 위치 지정 문제가 있습니다. 센터 (맨 위)에 레이블을 넣으려고 시도하지만 (내 눈에 보이지 않는 이유로) 코드를 사용하려고합니다. : enter image description hereGridBagLayout을 사용하여 위치 지정

Test라는 레이블이 내 창 상단과 중앙에 있어야합니다. 누군가 나에게이 나쁜 위치에 대한 이유를 설명 할 수 있니?

내 프로그램 :

public class Accueil extends JFrame { 

    private JPanel home = new JPanel(); 
    private GridBagConstraints grille = new GridBagConstraints(); 
    private JLabel title = new JLabel("Test"); 

    public Accueil() { 
     home.setLayout(new GridLayout()); 
     init_grille(); 
     init_title(); 

     this.add(home); 
     this.setSize(600,600); 
     this.setTitle("Test One"); 
     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 

    } 

    private void init_grille() { 
     grille.fill = GridBagConstraints.BOTH; 
     grille.weightx = 2; 
     grille.weighty = 5; 
     grille.ipady=grille.anchor=GridBagConstraints.CENTER;; 

    } 

    private void init_title() { 
     grille.fill = GridBagConstraints.HORIZONTAL; 
     grille.gridx = 0; 
     grille.gridy = 0; 
     home.add(title,grille); 
    } 

    public static void main(String [] args) { 
     new Accueil(); 
    } 
} 
+5

GridBagLayout을 사용하지 않고 GridLayout을 사용하고 있습니다. –

+0

GridLayout을 사용하려는 경우 태그를 변경해야합니다. –

답변

1

이 도움이되지 않습니다 :

home.setLayout(new GridLayout()); 

을 당신은 아마 원하는 :

home.setLayout(new GridBagLayout()); 
또한

는, 이러한 변경 작업을해야합니다

private void init_title() { 
     grille.fill = GridBagConstraints.NONE; 
     grille.gridx = 0; 
     grille.gridy = 0; 
     grille.anchor = GridBagConstraints.NORTH; 
     home.add(title,grille); 
    } 
+0

GridBagLayout과 GridLayout 사이에서 "가장"입니까? 나는 다른 compenents의 배치의 의미에서 "최고"를 의미합니다 – afk

+1

최고가 없습니다. 간단한 그리드가 필요하면 GridLayout이 가장 적합하고 사용하기가 더 쉽습니다. 좀 더 복잡한 것을 필요로 할 때, GridLayout은 할 수없고, GridBagLayout은 더 복잡 할 수 있습니다. –

+0

+1 to JB. 보유하고있는 구성 요소 나 배치 할 위치에 대한 최종 목표를 모르기 때문에 기본 LayoutManager 제안은 추측에 불과합니다. – splungebob

관련 문제