2016-09-21 99 views
0

我用4個JPanel創建了一個JFrame。使用GridBagLayout,我創建了三行兩列,每列兩個面板。通過將藍色面板的cellheight從1更改爲2,我可以覆蓋它下面的單元格。我想爲綠色面板做同樣的事情,以填充它下面的空間。下面是我的代碼目前生產:GridBagLayout - 我如何讓這個單元格填充它下面的空間?

enter image description here

我試圖改變綠色面板的gridheight爲2,但我結束了這一點:

enter image description here

我使用的GridBagLayout不正確?這樣做的適當方式是什麼?

這裏是我的代碼:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     addComponents(frame.getContentPane()); 
     frame.setSize(800, 600); 
     frame.setVisible(true); 
    } 

    public static void addComponents(Container contentPane) { 
     contentPane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.insets = new Insets(3,3,3,3); 
     c.fill = GridBagConstraints.BOTH; 

     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     c.gridheight = 2; 
     c.weightx = 1; 
     c.weighty = 0.85; 
     JPanel panel1 = new JPanel(); 
     panel1.setBackground(Color.BLUE); 
     contentPane.add(panel1, c); 

     c.gridx = 0; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 1.1; 
     c.weighty = 0.35; 
     JPanel panel2 = new JPanel(); 
     panel2.setBackground(Color.YELLOW); 
     contentPane.add(panel2, c); 

     c.gridx = 1; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 0.15; 
     c.weighty = 0.5; 
     JPanel panel3 = new JPanel(); 
     panel3.setBackground(Color.RED); 
     contentPane.add(panel3, c); 

     c.gridx = 1; 
     c.gridy = 1; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 0.38; 
     c.weighty = 0.5; 
     JPanel panel4 = new JPanel(); 
     panel4.setBackground(Color.GREEN); 
     contentPane.add(panel4, c); 
    } 
} 
+0

行跨度爲更好地幫助越早第三列如預期發佈[MCVE ]或[簡短,獨立,正確的例子](http://www.sscce.org/)。 –

+0

謝謝安德魯。我添加了其餘的代碼。 –

+0

注意:如果有三個單元格的第三列,每個單元格的行跨距爲1,它就會像預期那樣工作。 –

回答

2

創建三行兩列,

實際上你只有兩行,因爲你永遠只添加成分爲0的gridy值和1.其中一個組件的gridheight爲2的事實不會創建一個新行。

一個解決方案是使用嵌套面板。

爲藍色和黃色組件創建一個面板。這將使用具有一列和兩行的GridBagLayout。然後,您可以設置每個組件的重量值以給出所需的高度。

然後,再次爲1列和2行創建紅色和綠色組件的第二個面板。重量將被設置爲0.5。

最後,您將創建第二個面板,其中包含兩列和一行。您可以設置所需的weightx並將以上兩個面板添加到該面板,然後將該面板添加到該框架。

+0

我能夠通過嵌套面板獲得我想要的格式。謝謝!但是你說我只「添加了一個格子值爲0和1的組件」 - 我的黃色面板使用的格子值爲2. –

+0

@TomBrannan,對不起,我的意思是你不能只填寫網格值。你不能有一個0和一個10的網格,並期望佈局管理器爲這兩個值之間的其他9個網格插入空間。這就是爲什麼你需要像我建議的那樣嵌套面板,或者像Andrew建議的那樣添加虛擬組件來佔據每個網格。 – camickr

2

佈局開始出現,如果有三個單元,每一個具有1

enter image description hereenter image description here

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

public class AnotherFourPanelLayout { 

    private JComponent ui = null; 

    AnotherFourPanelLayout() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui != null) { 
      return; 
     } 

     ui = new JPanel(new BorderLayout(4, 4)); 
     ui.setBorder(new EmptyBorder(4, 4, 4, 4)); 

     addComponents(ui); 
    } 

    public void addComponents(Container contentPane) { 
     contentPane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.insets = new Insets(3, 3, 3, 3); 
     c.fill = GridBagConstraints.BOTH; 

     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     c.gridheight = 2; 
     c.weightx = 0.66; 
     c.weighty = 0.66; 
     JPanel panel1 = new JPanel(); 
     addLabel(panel1, c); 
     panel1.setBackground(Color.CYAN); 
     contentPane.add(panel1, c); 

     c.gridx = 0; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     //c.weightx = 1.1; // logical? 
     c.weightx = 0.66; 
     c.weighty = 0.33; 
     JPanel panel2 = new JPanel(); 
     addLabel(panel2, c); 
     panel2.setBackground(Color.YELLOW); 
     contentPane.add(panel2, c); 

     c.gridx = 1; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 0.33; 
     c.weighty = 0.33; 
     JPanel panel3 = new JPanel(); 
     addLabel(panel3, c); 
     panel3.setBackground(Color.RED); 
     contentPane.add(panel3, c); 

     c.gridx = 1; 
     c.gridy = 1; 
     c.gridwidth = 1; 
     c.gridheight = 2; 
     c.weightx = 0.33; 
     c.weighty = 0.66; 
     JPanel panel4 = new JPanel(); 
     addLabel(panel4, c); 
     panel4.setBackground(Color.GREEN); 
     contentPane.add(panel4, c); 

     // hack to fix? 
     c.gridx = 2; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 0.01; 
     c.weighty = 0.33; 
     JPanel panelH1 = new JPanel(); 
     //addLabel(panelH1, c); 
     panelH1.setBackground(Color.MAGENTA); 
     contentPane.add(panelH1, c); 

     c.gridy = 1; 
     JPanel panelH2 = new JPanel(); 
     //addLabel(panelH2, c); 
     panelH2.setBackground(Color.MAGENTA); 
     contentPane.add(panelH2, c); 

     c.gridy = 2; 
     JPanel panelH3 = new JPanel(); 
     //addLabel(panelH3, c); 
     panelH3.setBackground(Color.MAGENTA); 
     contentPane.add(panelH3, c); 
    } 

    private void addLabel(JPanel panel, GridBagConstraints gbc) { 
     panel.add(new JLabel(constraintsToString(gbc))); 
    } 

    private String constraintsToString(GridBagConstraints gbc) { 
     StringBuilder sb = new StringBuilder(); 

     sb.append("<html><table>"); 
     sb.append(addRowToTable("Grid X", gbc.gridx)); 
     sb.append(addRowToTable("Grid Y", gbc.gridy)); 
     sb.append(addRowToTable("Weight X", gbc.weightx)); 
     sb.append(addRowToTable("Weight Y", gbc.weighty)); 
     sb.append(addRowToTable("Grid Width", gbc.gridwidth)); 
     sb.append(addRowToTable("Grid Height", gbc.gridheight)); 

     return sb.toString(); 
    } 

    private String addRowToTable(String label, double value) { 
     StringBuilder sb = new StringBuilder("<tr><td>"); 

     sb.append(label); 
     sb.append("</td><td>"); 
     sb.append(value); 
     sb.append("</td></tr>"); 


     return sb.toString(); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       AnotherFourPanelLayout o = new AnotherFourPanelLayout(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
相關問題