2012-04-21 5 views
1

모든 것이 가운데쪽으로 갈겨 대는 것 같지만 왜 그럴 수 없습니까. 여기 내 코드가있다. 가능한 한 쉽게 읽을 수 있도록 노력했습니다.Java GridBagLayout 레이아웃 문제

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.DefaultListModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JTextField; 

/** 
* 
* @author rubixibuc 
*/ 
public class RulesWindow extends JFrame { 

    public GridBagLayout layout = new GridBagLayout(); 
    public GridBagConstraints constr = new GridBagConstraints(); 
    public DefaultComboBoxModel modelE = new DefaultComboBoxModel(); 
    public DefaultComboBoxModel modelA = new DefaultComboBoxModel(); 
    public DefaultListModel modelL = new DefaultListModel(); 

    public RulesWindow() 
    { 
     super("Rules Window"); 
     /* layout for window 
     |_P_|__E__|_T_|__A__|_a_| 
     |     |xxx| 
     |     |xxx| 
     |     |xxx| 
     |_________L_________|_M_| 
     X = no fill 
     P = pattern field 
     E = extension combobox 
     T = target action 
     A = action combobox 
     a = add rule 
     M = remove rule 
     L = rule list 
     */ 

     setLayout(layout); 

     JTextField P = new JTextField(); // 0,0,1,1 
     JComboBox E = new JComboBox(modelE); // 1,0,2,1 
     JTextField T = new JTextField(); // 3,0,1,1 
     JComboBox A = new JComboBox(modelA); // 4,0,2,1 
     JButton a = new JButton("+"); // 6,0,1,1 
     JList L = new JList(modelL); // 1,1,6,4 
     JButton M = new JButton("-"); // 6,4,1,1 

     changeConstraints(0,0,1,1); 
     add(P); 
     changeConstraints(1,0,2,1); 
     add(E); 
     changeConstraints(3,0,1,1); 
     add(T); 
     changeConstraints(4,0,2,1); 
     add(A); 
     changeConstraints(6,0,1,1); 
     add(a); 
     changeConstraints(1,1,6,4); 
     add(L); 
     changeConstraints(6,4,1,1); 
     add(M); 

     setSize(200,200); 
     setVisible(true); 
     setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    } 
    public final void changeConstraints(int gridx, int gridy, int gridwidth, int gridheight) 
    { 
     constr.anchor = GridBagConstraints.NORTHEAST; 
     constr.fill = GridBagConstraints.BOTH; 
     constr.gridx = gridx; 
     constr.gridy = gridy; 
     constr.gridwidth = gridwidth; 
     constr.gridheight = gridheight; 
    } 
    @Override 
    public final Component add(Component comp) 
    { 
     layout.setConstraints(comp, constr); 
     super.add(comp); 
     return comp; 
    } 
} 

미리 감사드립니다. 저는 코멘트에서 어떻게 보이는지를 도표로 나타 냈습니다.

또한 오버라이드 된 메서드를 올바르게 호출하는 방법입니까?

+0

방금 ​​마지막 두 가지 방법을 변경하여 나중에 문제가 발생할 것이라고 생각했습니다. – rubixibuc

답변

2

때마다 여기

그것은 모든 중심을 향해 첨벙 첨벙 것 같다,하지만 weightx에는 및 무게 제약 곳의 GridBagLayout와 관련

, 내가 물어 왜 알아낼 수 없습니다 I ?

가능한 빠른 해결책 : GridBagConstraints 가중치 및 중요도 필드를 적절한 값으로 설정해야합니다 (기본값을 1.0으로 설정하여 시작하고 그 다음에 재생할 수 있음).

+0

필자는 필자가 필 요 없다고 생각하고 앵커는 디버깅을 위해 왼쪽 상단에 있지만 여전히 중앙에 찌그러 뜨 렸습니다. – rubixibuc

+0

기본 값이 좋을 것이라고 생각했습니다. – rubixibuc

+0

@rubixibuc : 온라인에서 사용할 수있는 자습서를 읽었습니까? 그들은 매우 도움이되며 왜 0.0의 기본값이 좋지 않은지 알려줄 것입니다. –