2013-11-21 169 views
1

我在我的父面板中使用gridbaglayout來設置與窗口大小成比例的重量大小。 然後我在裏面添加面板,並將其佈局設置爲grouplayout.it覆蓋我在父級設置的權重,並佔用比預期的更多空間。如果我更改了grouplayout中的值,它會被修復,但我想知道爲什麼它會覆蓋父面板指定的大小?爲什麼組合播放會調整父網格佈局佈局的大小?

這裏是我的代碼和圖片:

GridBagLayout gbl_panelMain = new GridBagLayout(); 
    gbl_panelMain.columnWeights = new double[]{0.05,0.9,0.05}; 
    gbl_panelMain.rowWeights = new double[]{0.24,0.72,0.04}; 
    panelMain.setLayout(gbl_panelMain); 
    panelContent = new JPanel(); 
    panelContent.setOpaque(false); 
    GridBagConstraints gbc_panel_2 = new GridBagConstraints(); 
    gbc_panel_2.insets = new Insets(0, 0, 0, 0); 
    gbc_panel_2.fill = GridBagConstraints.BOTH; 
    gbc_panel_2.gridx = 1; 
    gbc_panel_2.gridy = 1; 
    panelMain.add(panelContent, gbc_panel_2); 

之前,我添加的GroupLayout:

enter image description here

GroupLayout gl_panelContent = new GroupLayout(panelContent); 
    gl_panelContent.setHorizontalGroup(
     gl_panelContent.createParallelGroup(Alignment.LEADING) 
      .addGap(0, 253, Short.MAX_VALUE) 
    ); 
    gl_panelContent.setVerticalGroup(
     gl_panelContent.createParallelGroup(Alignment.LEADING) 
      .addGap(0, 358, Short.MAX_VALUE) 
    ); 
    panelContent.setLayout(gl_panelContent); 

後:

enter image description here

回答

0

顯然Grouplayout不尊重其父級大小限制。當我用GroupLayout.Preferred_Size替換下列行中的固定大小時,問題就解決了。

.addGap(0, 253, Short.MAX_VALUE) 
.addGap(0, 358, Short.MAX_VALUE)