So what I'm trying to do is: 
I am using grid layout, and here is what I still have:
public class board { public static void addComponentsToPane(Container pane) { pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JPanel leftTop = new JPanel(); leftTop.setPreferredSize(new Dimension(251,300)); leftTop.setBackground(Color.black); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; pane.add(leftTop, c); JPanel middleTop = new JPanel(); middleTop.setPreferredSize(new Dimension(251,200)); middleTop.setBackground(Color.green); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; pane.add(middleTop, c); JPanel rightTop = new JPanel(); rightTop.setPreferredSize(new Dimension(251,600)); rightTop.setBackground(Color.blue); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 2; c.gridy = 0; pane.add(rightTop, c); JPanel leftBottom = new JPanel(); leftBottom.setPreferredSize(new Dimension(251,300)); leftBottom.setBackground(Color.red); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 1; pane.add(leftBottom, c); JPanel middleBottom = new JPanel(); middleBottom.setPreferredSize(new Dimension(251,400)); middleBottom.setBackground(Color.yellow); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; pane.add(middleBottom, c); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
It creates something like: 
How would I press the panels so that they touch each other, as in the first picture. I looked at GridBagConstraints, but I could not find anything like this to work. Thanks!
source share