bijiDango
bijiDango

Reputation: 1596

How may I adjust border layout as the image show?

enter image description here

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

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

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.,

enter image description here

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

Related Questions