Reputation: 1596
I don't know how to describe this question, so I didn't search SO or Google. Anyone has a good idea for naming this question please tell me.
I know that adding a extra JPanel would work, I wonder if we can just adjust the layout.
Upvotes: 0
Views: 278
Reputation: 285403
Nest two BorderLayout using JPanels. The outer one has the two side JPanels and the center "column" JPanel that holds three JPanels. Then this center column JPanel uses BorderLayout and holds the top, bottom and center JPanels.
e.g.,
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class MyLayouts {
private static void createAndShowGui() {
JPanel centerColumnPanel = new JPanel(new BorderLayout());
centerColumnPanel.setBorder(BorderFactory.createTitledBorder("Center Column Panel"));
centerColumnPanel.add(makePanel("North", 400, 100), BorderLayout.PAGE_START);
centerColumnPanel.add(makePanel("South", 400, 100), BorderLayout.PAGE_END);
centerColumnPanel.add(makePanel("Center", 400, 200), BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createTitledBorder("Main Panel"));
mainPanel.add(centerColumnPanel, BorderLayout.CENTER);
mainPanel.add(makePanel("West", 100, 400), BorderLayout.LINE_START);
mainPanel.add(makePanel("East", 100, 400), BorderLayout.LINE_END);
JFrame frame = new JFrame("MyLayouts");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private static JPanel makePanel(String title, int width, int height) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(title));
panel.add(Box.createRigidArea(new Dimension(width, height)));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 3