I have a little issue with the GridBag Layout Manager. I am trying to display 9 panels like this:
I have tried using BorderLayout
to check how it works. But just posting here .. :) This process uses multiple panels inside each other. You can try this approach if this example is comfortable.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class BorderAndGridBag {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new GridWithBorder().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class GridWithBorder {
public void createUI() {
JFrame frame = new JFrame();
JPanel motherPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel topLeftPanel = new JPanel();
topLeftPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel topRightPanel = new JPanel();
topRightPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel topMiddlePanel = new JPanel();
topMiddlePanel.add(new JLabel("Top Middle"));
topMiddlePanel.setBorder(LineBorder.createBlackLineBorder());
JPanel bottomLeftPanel = new JPanel();
bottomLeftPanel.add(new JLabel("Bottom Left"));
bottomLeftPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel bottomRightPanel = new JPanel();
bottomRightPanel.add(new JLabel("Bottom Right"));
bottomRightPanel.setBorder(LineBorder.createBlackLineBorder());
JPanel topLeftLeft = new JPanel();
topLeftLeft.add(new JLabel("Top Left"));
topLeftLeft.setBorder(LineBorder.createBlackLineBorder());
JPanel topLeftRight = new JPanel(new BorderLayout());
JPanel topLeftRightTop = new JPanel();
topLeftRightTop.add(new JLabel("Top Left's Right Top"));
topLeftRight.setBorder(LineBorder.createBlackLineBorder());
JPanel topLeftRightBottom = new JPanel();
topLeftRightBottom.add(new JLabel("Top Left's Right Bottom"));
JPanel topRightLeft = new JPanel(new BorderLayout());
topRightLeft.setBorder(LineBorder.createBlackLineBorder());
JPanel topRightRight = new JPanel();
topRightRight.add(new JLabel("Top Right Right"));
topRightRight.setBorder(LineBorder.createBlackLineBorder());
JPanel topRightLeftTop = new JPanel();
topRightLeftTop.add(new JLabel("Top Right Left Top"));
JPanel topRightLeftBottom = new JPanel();
topRightLeftBottom.add(new JLabel("Top Right Left Bottom"));
topLeftPanel.add(topLeftLeft, BorderLayout.WEST);
// Top panel sub alignment.
topLeftPanel.add(topLeftRight, BorderLayout.EAST);
topLeftRight.add(topLeftRightTop, BorderLayout.NORTH);
topLeftRight.add(topLeftRightBottom, BorderLayout.CENTER);
topRightLeft.add(topRightLeftTop, BorderLayout.NORTH);
topRightLeft.add(topRightLeftBottom, BorderLayout.SOUTH);
topRightPanel.add(topRightLeft, BorderLayout.WEST);
topRightPanel.add(topRightRight, BorderLayout.EAST);
// Top Panel main alignment.
topPanel.add(topLeftPanel, BorderLayout.WEST);
topPanel.add(topMiddlePanel, BorderLayout.CENTER);
topPanel.add(topRightPanel, BorderLayout.EAST);
// Bottom Panel main alignment.
bottomPanel.add(bottomLeftPanel, BorderLayout.WEST);
bottomPanel.add(bottomRightPanel, BorderLayout.EAST);
motherPanel.add(topPanel, BorderLayout.NORTH);
motherPanel.add(bottomPanel, BorderLayout.CENTER);
frame.add(motherPanel);
frame.pack();
frame.setTitle("Layout Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}