当前位置: 首页 > 知识库问答 >
问题:

如何使用MigLayout将内容集中在多个面板上?

孟浩然
2023-03-14

我有一个大面板,其中包含3个较小的面板(LocationPanelUsagePanelStructuralAspectSpanel)。

每个较小的面板上都有一些JLabelsJCheckBoxes。我继续和中心的组件在每个面板上,但我如何完成中心他们在所有3个面板?(请看中间黑线)

我曾尝试在miglayout(new JLabel(“label here”),“cell0”)中使用cell-layout选项,但无法动态创建相同大小的间隙,从而使所有组件都居中。使用gap200(180,300,...)将组件“推入”视觉中心似乎是可行的,但我不想使用绝对定位/间隙,因为它们可能很容易断裂。

public class RiskAssessmentPage extends JPanel {
  JPanel riskAssessmentPanel = new JPanel();

  JPanel locationPanel = new JPanel();
  JPanel usagePanel = new JPanel();
  JPanel structuralAspectsPanel = new JPanel();

  public RiskAssessmentPage() {
    setLayout(new MigLayout(""));
    riskAssessmentPanel.setLayout(
        new MigLayout("wrap", "[grow, fill]", "[grow, fill, push][grow, fill, push][grow, fill, push]"));

    locationPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
    locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));

    usagePanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
    usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));

    structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
    structuralAspectsPanel.setBorder(BorderFactory.createTitledBorder("Structural Aspects"));

    locationPanel.add(new JLabel("This is the first of all labels"));
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JSeparator(), "growx, span");
    locationPanel.add(new JLabel("Second Label"));
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JSeparator(), "growx, span");
    locationPanel.add(new JLabel("This Label is fairly large and long and pushes the text around"));
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

    usagePanel.add(new JLabel("A label in the usage panel"));
    usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JSeparator(), "growx, span");
    usagePanel.add(new JLabel("And another one and another one and another one"));
    usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

    structuralAspectsPanel.add(new JLabel("Label here"));
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JSeparator(), "growx, span");
    structuralAspectsPanel.add(new JLabel("I am so uncreative with label names..."));
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JSeparator(), "growx, span");
    structuralAspectsPanel.add(new JLabel("Thats it. I give up with naming them."));
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

    riskAssessmentPanel.add(locationPanel);
    riskAssessmentPanel.add(usagePanel);
    riskAssessmentPanel.add(structuralAspectsPanel);

    add(riskAssessmentPanel, "grow, push");
  }
}

共有1个答案

公良鸿风
2023-03-14

为了使面板列对齐,您需要将它们设置为相同的大小。在miglayout中,您可以通过将第一列的首选大小和增长率设置为与所有面板中的值相同来完成此操作。例如

panel1.new MigLayout("", //Unchanged Layout Constraints
        "[:500, grow, center][grow, left]", //Column Constraints
        ""); //Unchanged Row Constraints

panel2.new MigLayout("", //Unchanged Layout Constraints
        "[:500, grow, center][grow, left]", //Column Constraints
        ""); //Unchanged Row Constraints

在本例中,panel1panel2第一列的首选大小都设置为500,增长设置为默认值,因此也是相同的。

这里可以看到所描述的一个工作示例

import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class MigLay extends JFrame {

    private MigLay() {
        super("Button Layout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new MigLayout("wrap", //Layout Constraints
        "grow, fill", //Column Constraints
        "grow, fill")); //Row Constraints
        createPanels();
        pack();
        setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen
        setVisible(true);
    }

    private void createPanels() {

        JPanel locationPanel = new JPanel();
        JPanel usagePanel = new JPanel();
        JPanel structuralAspectsPanel = new JPanel();

        //JLabels for font metrics
        JLabel one = new JLabel("This is the first of all labels");
        JLabel two = new JLabel("Second Label");
        JLabel three = new JLabel("This Label is fairly large and long and pushes the text around");
        JLabel four = new JLabel("A label in the usage panel");
        JLabel five = new JLabel("And another one and another one and another one");

        //Font Metrics
        FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code)
        int width = metrics.stringWidth(three.getText());

        locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
        locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));

        usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
        usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));

        locationPanel.add(one);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JSeparator(), "growx, span");
        locationPanel.add(two);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JSeparator(), "growx, span");
        locationPanel.add(three);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

        usagePanel.add(four);
        usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JSeparator(), "growx, span");
        usagePanel.add(five);
        usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

        getContentPane().add(locationPanel);
        getContentPane().add(usagePanel);
    }

    public static void main(String[] args) {
        new MigLay();
    }
}
 类似资料:
  • 我很好奇,在放置组件时,如何使用miglaway在网格上均匀分布组件: 例如,如果我在一个按钮组中放置四个JRadioButton以跨越3行2列(2,3),然后放置三个JButton,则分布的权重是不相等的,我最终会得到以下结果: 如图所示,A和B的大小相同,C保留剩余的空间:我希望A、B、C在垂直方向上等于三分之一。 如果这在GridBagLayout中是可能的,我也可以利用它,但是我在任何一种

  • 问题内容: 我正在测试像Windows Metro风格的中心分隔线。如果您检查以下代码: 灰色框为70%,在屏幕上居中,这是正确的,但是当我将窗口加宽并且绿色分隔线在移动时,您会看到绿色框在某些点上没有居中。 我已经整天在搜索这个了 如何帮助我呢? 问题答案: 我之前的答案显示了一种坦率地说已经过时的方法(它仍然有效,只有更好的方法可以实现此目的)。因此,我正在对其进行更新,以包括一个更现代的fl

  • 我有上面的编码来提取使用硒 - 蟒蛇的href链接。我想提取每个人的个人资料“董事会成员”中的内容。我知道如何逐个提取它们,但不知道如何编写循环来执行此操作。 以下是我的代码: 任何想法都很感谢!

  • 了解如何通过映射实时视图中的元素或 CSS Designer 中应用的选择器及其 HTML 标记,使用 DOM 面板来编辑 HTML 结构。 DOM 面板呈现包含静态和动态内容的交互式 HTML 树。此视图有助于直观地在实时视图中通过 HTML 标记以及 CSS Designer 中所应用的选择器,对元素进行映射。您也可在 DOM 面板中编辑 HTML 结构,并在实时视图中查看即时生效的更改。 要

  • 问题内容: 我正在使用一个使用Swing的旧版应用程序,我正在努力弄清用户单击按钮时屏幕的变化情况。我无法弄清楚的原因之一是因为这是我第一次使用Swing。我读了一本书,掌握了基础知识,但仍然很挣扎。 基本上,我正在查看的屏幕具有一个JSplitPane,其左侧具有多个快捷按钮,而右侧具有一个空白窗格。当我单击该按钮时,根据所按下的按钮,右侧窗格中会显示一个不同的屏幕。 遍历代码,我期望在某处会有

  • 问题内容: 我想将五个文件的内容照原样复制到一个文件中。我尝试使用cp为每个文件执行此操作。但这会覆盖从先前文件复制的内容。我也试过 它没有用。 我希望我的脚本在每个文本文件的末尾添加换行符。 例如。文件1.txt,2.txt,3.txt。将1,2,3的内容放入0.txt 我该怎么做 ? 问题答案: 您需要(concatenate的简称)命令,并使用shell重定向()进入输出文件