2014-12-02 5 views
3

JFrame에서 폭이 다른 두 개의 패널을 만들려고합니다. 오른쪽 패널의 왼쪽 패널 두께가 두 배가되기를 바랍니다. 내 leftpanel에 gridwidth = 1, rightboardel에 gridwidth = 2를 사용하여 GridBagLayout을 사용하려고합니다. 하지만 너비가 달라지는 대신 동일한 너비의 패널이 두 개 있습니다. 무엇을 바꾸어야합니까? 어떤 도움을 주시면 감사하겠습니다.JFrame에서 GridBagLayout을 사용하여 여러 너비의 JPanel

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

public class AppFrame2 extends JFrame { 

    GridBagLayout gbl; 
    GridBagConstraints gbc; 
    JPanel leftpanel; 
    JPanel rightpanel; 

    public AppFrame2() { 
     this.setSize(500, 500); 
     this.setResizable(false); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
     this.getContentPane().setBackground(Color.BLACK); 

     // Settning Frame Layout 
     gbl = new GridBagLayout(); 
     gbc = new GridBagConstraints(); 
     this.setLayout(gbl); 

     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 

     // Add Panels 
     leftpanel = new JPanel(); 
     leftpanel.setBackground(Color.green); 
     this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component 

     rightpanel = new JPanel(); 
     rightpanel.setBackground(Color.red); 
     this.addPanels(0, 1, 1, 2, rightpanel); 
    } 

    private void addPanels(int row, int col, int height, int width, 
     Component com) { 
     gbc.gridx = col; 
     gbc.gridy = row; 
     gbc.gridheight = height; 
     gbc.gridwidth = width; 
     gbl.setConstraints(com, gbc); 
     this.getContentPane().add(com); 
    } 

    public static void main(String[] args) { 
     new AppFrame2(); 
    } 

} 
+0

내부 패딩 사용을 고려 했습니까? – TNT

+0

크기가 조정되는 이유는 GridBagLayout이 사물을 같은 크기로 참조하기 때문입니다. 더 좋은 예를 들어 이것을 참조하십시오 http://stackoverflow.com/questions/4112886/components-in-gridlayout-wit-jpanel-fills-the-grid-inclurectly – Dacotah

+0

gbc.insets (2, 2, 2, 2)를 사용했습니다. , 그러나 나는 그것을 제거했다. 나는 다른 패딩에 대해 모른다. 예를 들어 주시겠습니까? –

답변

4

문제는 사용자가 너비를 지정하지 않는다는 것입니다. gbl.columnWidths = new int[] {500/3, 500/3*2}; 줄을 추가하면 열의 너비가 원하는대로 설정됩니다. 여기에 코드를 추가하여 작동하게했습니다.

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

public class AppFrame2 extends JFrame { 

    GridBagLayout gbl; 
    GridBagConstraints gbc; 
    JPanel leftpanel; 
    JPanel rightpanel; 

    public AppFrame2() { 
     this.setSize(500, 500); 
     this.setResizable(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
     this.getContentPane().setBackground(Color.BLACK); 

     // Settning Frame Layout 
     gbl = new GridBagLayout(); 
     //************* New code **************// 
     gbl.columnWidths = new int[] {500/3, 500/3*2}; 
     gbl.rowHeights = new int[] {500}; 
     gbl.columnWeights = new double[] {1, 1}; 
     gbl.rowWeights = new double[] {1}; 
     //************* End code **************// 

     gbc = new GridBagConstraints(); 
     this.setLayout(gbl); 

     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 

     // Add Panels 
     leftpanel = new JPanel(); 
     leftpanel.setBackground(Color.green); 
     this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component 

     rightpanel = new JPanel(); 
     rightpanel.setBackground(Color.red); 
     this.addPanels(0, 1, 1, 2, rightpanel); 
    } 

    private void addPanels(int row, int col, int height, int width, 
          Component com) { 
     gbc.gridx = col; 
     gbc.gridy = row; 
     gbc.gridheight = height; 
     gbc.gridwidth = width; 
     gbl.setConstraints(com, gbc); 
     this.getContentPane().add(com); 
    } 

    public static void main(String[] args) { 
     new AppFrame2(); 
    } 

} 
+0

지정하는 것을 잊어 버린 한 가지 :이 코드 줄을 추가하면 제약 조건을 추가 할 그리드 위치를 알 수 있습니다. 지정하지 않으면 사양이없는 위치가 만들어집니다. –

+1

효과가있었습니다. : D 당신은 나의 날을 구했습니다. 도와 주셔서 감사합니다. –

+0

문제가 없습니다! = D 언제든지. –