2011-08-02 2 views
1

3 개의 JPanels가 있으며 하나의 JPanel에 모두 배치하려고합니다. 메인 패널에 GridBagLayout을 사용했습니다. 그러나 단 하나의 패널 만 추가되고 있습니다. 왜 이것이 될 수 있을까요?메인 패널에 3 개의 JPanels를 추가 할 수 없습니다.

gblayout=new GridBagLayout(); 
    gbc=new GridBagConstraints(); 
    panel1Customizer(); 
    panel2customizer(); 
    panel3Customizer(); 
    setLayout(gblayout); 
    gbc.fill=GridBagConstraints.HORIZONTAL; 
    gbc.anchor=GridBagConstraints.NORTHWEST; 
    gbc.weightx=1; 
    gbc.weighty=1; 
    gbc.gridheight=GridBagConstraints.REMAINDER; 
    add(panel1, gbc); 
    add(panel2, gbc); 
    gbc.gridwidth=GridBagConstraints.REMAINDER; 
    add(panel3, gbc); 

사용자 지정자 메서드는이 패널에 항목을 추가하는 메서드입니다.

+0

, 문제가 아마 다른 곳이다. –

답변

1

잘 모르겠지만 GridBagLayout에 GridBagConstraints를 추가해야한다고 생각합니다. 이 사이트에서 찾아보십시오 것은 GridBagLayout에 작동하는 방법에 대한 아이디어를 얻을 수 있습니다 : 는 link

아니면 그냥 제대로

1

당신은 GBC를 변경해야 어쩌면의 BorderLayout 또는 GridLayout과 당신의 패널을 준비하려면 JFrame의 또 다른 레이아웃을 사용합니다. 는 gridx 및/또는 gbc.gridy는 How to Use GridBagLayout을 읽을 필요

+1

실제로 필요하지는 않지만 기본적으로 구성 요소가 추가됩니다. –

1

, 그 hereGridBagConstraints에 대한 예, 당신의 gbc.fill=GridBagConstraints.HORIZONTAL;을 변경하려면 JComponent's Size과 문제 (들)이있는 경우 다음 setPreferedSize()를 추가 할 각 패널에 대해 서로 다른하는 단계; 예를

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.*; 

public class GBLFillBoth extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public GBLFillBoth() { 
    JPanel panel = new JPanel(); 
    GridBagLayout gbag = new GridBagLayout(); 
    panel.setLayout(gbag); 
    GridBagConstraints c = new GridBagConstraints(); 
    JButton btn1 = new JButton("One"); 
    c.fill = GridBagConstraints.BOTH; 
    //c.fill = GridBagConstraints.HORIZONTAL; 
    c.anchor=GridBagConstraints.NORTHWEST; 
    c.gridx = 0; 
    c.gridy = 0; 
    c.weightx = 0.5; 
    c.weighty = 0.5; 
    panel.add(btn1, c); 
    JButton btn2 = new JButton("Two"); 
    c.gridx++; 
    panel.add(btn2, c); 
    //c.fill = GridBagConstraints.BOTH; 
    JButton btn3 = new JButton("Three"); 
    c.gridx = 0; 
    c.gridy++; 
    c.gridwidth = 2; 
    panel.add(btn3, c); 
    add(panel); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pack(); 
    setVisible(true); 
    } 

    public static void main(String[] args) { 
     GBLFillBoth gBLFillBoth = new GBLFillBoth(); 
    } 
} 
1

당신은 대신 MigLayout 사용을 고려할 수에 대한 코드는 매우 간단하다 :이 코드는 잘 작동

panel1Customizer(); 
panel2customizer(); 
panel3Customizer(); 
setLayout(new MigLayout("fill, wrap 3")); 

add(panel1, "grow"); 
add(panel2, "grow"); 
add(panel3, "grow"); 
관련 문제