2013-12-13 3 views
2

버튼과 레이블을 이동하려고합니다. 버튼을 오른쪽 아래 모서리에 놓고 라벨을 바로 위에두기를 원하지만, 레이아웃 시스템을 어떻게 변경하든 모두 같은 줄에 붙어서 전혀 움직이지 않을 것입니다. 어떻게 움직이게할까요?GridBagLayout 요소가 움직이지 않습니다

 setLayout(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 

     gc.anchor = GridBagConstraints.CENTER; 
     gc.weighty = 1; 
     gc.gridx = 0; 
     gc.gridy = 0; 
     gc.ipadx = 5; 
     pane.add(explain_lbl, gc); 
     gc.ipadx = 1; 
     gc.gridy = 1; 
     gc.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     pane.add(btn_play_normal, gc); 
     gc.ipadx = 6; 
     gc.gridy = 1;; 
     gc.anchor = GridBagConstraints.PAGE_END; //bottom of space 

     gc.fill = GridBagConstraints.HORIZONTAL; 
     gc.ipady = 0;  //reset to default 
     gc.weighty = 1.0; //request any extra vertical space 
     gc.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     gc.insets = new Insets(10,0,0,0); //top padding 
     gc.gridx = 1;  //aligned with button 2 
     gc.gridwidth = 2; //2 columns wide 
     gc.gridy = 2;  //third row 
     pane.add(btn_play_ptr, gc); 
+0

더 도움이 빨리 들어, [SSCCE] (http://sscce.org/)을 게시 할 수 있습니다. –

+0

* "GridBagLayout 요소가 움직이지 않습니까?"* '?' 성명서에이 사실이 문제가되지는 않습니다. 내가 질문을 편집하여 하나 추가했습니다. [변경 내용을 신중하게 검토하십시오.] (http://stackoverflow.com/posts/20568108/revisions) & *** *** 질문이 아니라면 자신의 의견을 추가하십시오! –

답변

3

pane의 레이아웃을 GridBag 구성 요소를 수락하는 컨테이너로 설정하지 않습니다. 레이아웃이 작동하려면 레이아웃을 GridBagLayout으로 설정해야합니다.

특히 : 당신은 당신의 구성 요소를 추가하는

setLayout(new GridBagLayout()); // (A) 

// .... 

pane.add(btn_play_normal, gc); // (B) 

// ... 

pane.add(btn_play_ptr, gc); // (C) 

는 (B)와 (C)에서의 GridBagConstraints, GC를 사용하여 창,하지만 당신은 this 컨테이너의 레이아웃을 (설정을 한 것으로 나타났습니다 무엇을 (A)에 있지만, pane 컨테이너의 것은 아닙니다.

해결책 : 설정 창 레이아웃 :

pane.setLayout(new GridBagLayout()); 
+0

많은 감사합니다 :) 그것은 지금 작동합니다. – Paludan

+0

@ user2966573 : 때로는 우리 모두는 우리 코드를 살펴보기 위해 눈의 또 다른 세트 만 필요할뿐입니다. 천만에요. –