2016-09-21 58 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); 
    } 
} 
相关问题