Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are failing to specify any <code>weightx</code> and <code>weighty</code> values to the <code>GridBagConstraints</code>. Moreover your <code>gridwidth</code> values are wrong, since it only needs to be <code>2</code> for the bottom most <code>JPanel</code>, for the rest it needs to be <code>1</code>. </p> <hr> <p><strong>Explanation of what I am doing :</strong> Consider <code>JPanel</code>s <code>BLUE</code> and <code>RED</code>, they are to be placed along the <strong>X-AXIS</strong>, in the ratio <code>70:30</code>, with respect to each other (therefore their <code>weightx</code> will be <code>0.7</code> and <code>0.3</code> respectively. Since the total area along the <strong>X-AXIS</strong> is <code>1.0</code>). </p> <p>Now both of these <code>BLUE</code> and <code>RED JPanel</code>s are to be placed along the <strong>Y-AXIS</strong>, with respect to the third <code>GREEN JPanel</code> in the ratio <code>90:10</code>, therefore, both of these <code>BLUE</code> and <code>RED</code> will have <code>weighty = 0.9</code>, and the <code>GREEN JPanel</code> will have <code>weighty = 0.1</code>, but since <code>GREEN JPanel</code> is suppose to occupy the whole area (with respect to <strong>X-AXIS</strong>), as occupied by <code>BLUE</code> and <code>RED JPanel</code>s, for that matter, its <code>gridwidth = 2</code> and <code>weightx = 1.0</code>.</p> <hr> <p>Try this code example : </p> <pre><code>import java.awt.*; import javax.swing.*; public class GridBagLayoutExample { private GridBagConstraints gbc; public GridBagLayoutExample() { gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.FIRST_LINE_START; } private void displayGUI() { JFrame frame = new JFrame("GridBagLayout Example"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = getPanel(Color.WHITE); contentPane.setLayout(new GridBagLayout()); JPanel leftPanel = getPanel(Color.BLUE); JPanel rightPanel = getPanel(Color.RED); JPanel bottomPanel = getPanel(Color.GREEN.darker()); addComp(contentPane, leftPanel , 0, 0, 0.7, 0.9, 1, 1, GridBagConstraints.BOTH); addComp(contentPane, rightPanel , 1, 0, 0.3, 0.9, 1, 1, GridBagConstraints.BOTH); addComp(contentPane, bottomPanel , 0, 1, 1.0, 0.1, 2, 1, GridBagConstraints.BOTH); frame.setContentPane(contentPane); //frame.pack(); frame.setSize(300, 300); frame.setLocationByPlatform(true); frame.setVisible(true); } private void addComp(JPanel panel, JComponent comp , int gridX, int gridY , double weightX, double weightY , int gridWidth, int gridHeight, int fill) { gbc.gridx = gridX; gbc.gridy = gridY; gbc.weightx = weightX; gbc.weighty = weightY; gbc.gridwidth = gridWidth; gbc.gridheight = gridHeight; gbc.fill = fill; panel.add(comp, gbc); } private JPanel getPanel(Color backColour) { JPanel panel = new JPanel(); panel.setOpaque(true); panel.setBackground(backColour); panel.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5)); return panel; } public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { new GridBagLayoutExample().displayGUI(); } }; EventQueue.invokeLater(runnable); } } </code></pre> <p>Here is the OUTPUT of the same : </p> <p><img src="https://i.stack.imgur.com/KDqB1.png" alt="GridBagLayoutExample"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload