如何在图像显示时调整边框布局?

时间:2014-11-07 03:58:15

标签: java swing layout-manager border-layout

enter image description here

我不知道如何描述这个问题,所以我没有搜索SO或Google。任何人都有一个好主意来命名这个问题,请告诉我。

我知道添加额外的JPanel会起作用,我想知道我们是否可以调整布局。

1 个答案:

答案 0 :(得分:3)

使用JPanels嵌套两个BorderLayout。外面的那个有两个侧面的JPanels和中间的"列"持有三个JPanel的JPanel。然后这个中心列JPanel使用BorderLayout并保存顶部,底部和中心JPanel。

如,

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();
         }
      });
   }
}