【问题标题】:Which Layout Manager Should I use to achieve the following?我应该使用哪个布局管理器来实现以下目标?
【发布时间】:2015-03-27 09:24:11
【问题描述】:

我有一个JFrame 和三个JPanels。在框架上我使用了BorderLayout。在框架的CENTER 处,我放置了outerPanel。在我的outerPanel 上,我使用了MigLayout。另外两个面板被添加到outerPanel。这两个面板大小相等,它们的宽度加起来等于outerPanel 的宽度 - 我希望 outerPanel 分成两半。下面是代码:

public class ControlPanel extends JFrame {

// components

public JPanel outerPanel;
public JPanel innerPanel1;
public JPanel innerPanel2;

public ControlPanel() {
    this.createUI();
}

public void createUI() {
    // form properties
    this.setSize(new java.awt.Dimension(300, 300));
    this.setVisible(true);
    this.setLayout(new java.awt.BorderLayout());

    this.outerPanel = new JPanel();
    this.outerPanel.setPreferredSize(new java.awt.Dimension(260, 250));
    this.outerPanel.setLayout(new net.miginfocom.swing.MigLayout());
    this.outerPanel.setBorder(BorderFactory.createEtchedBorder());

    this.add(new javax.swing.JLabel("North"), BorderLayout.NORTH);
    this.add(this.outerPanel, BorderLayout.CENTER);

    this.innerPanel1 = new JPanel();
    this.innerPanel1.setPreferredSize(new java.awt.Dimension(130, 150));
    this.innerPanel1.setLayout(new net.miginfocom.swing.MigLayout());
    this.innerPanel1.setBorder(BorderFactory.createTitledBorder("Panel1"));

    this.innerPanel2 = new JPanel();
    this.innerPanel2.setPreferredSize(new java.awt.Dimension(130, 150));
    this.innerPanel2.setLayout(new net.miginfocom.swing.MigLayout());
    this.innerPanel2.setBorder(BorderFactory.createTitledBorder("Panel2"));

    this.outerPanel.add(this.innerPanel1);
    this.outerPanel.add(this.innerPanel2);
    this.pack();

    }

public static void main(String[] args) {

    ControlPanel cp = new ControlPanel();
  }
}

问题:当我运行我的程序时,在我调整窗口大小之前出现的 GUI 没有问题;但是当我调整窗口大小时 - 将其放大innerPane1innerPanel2 保持相同大小而不调整大小以占用可用空间

问题:我们如何使innerPannel1innerPanel2 这两个面板与窗口同时调整大小,以便它们可以平均共享可用空间?是否有任何特定的布局管理器,可用于将面板分成两等份,可以与窗口同时调整大小?

显示输出的图像。

  1. 调整大小之前 - GUI 看起来不错,面板大小正确。

  1. 调整大小后 - GUI 变形,面板大小不变。

【问题讨论】:

    标签: java swing layout-manager miglayout border-layout


    【解决方案1】:

    我建议你使用new GridLayout(1, 2)。这会将面板分成 1 行和 2(大小相同)列。

    所以,简单地改变

    this.outerPanel = new JPanel();
    

    this.outerPanel = new JPanel(new GridLayout(1, 2));
    

    应该这样做。

    【讨论】:

    • new GridLayout(1, 2)outterPanelJFrame 上?
    • 它完全按照我想要的方式工作。我会接受你的解决方案。
    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2018-05-02
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多