如何消除MigLayout中两个单元格之间的垂直间隙? [英] How to remove vertical gap between two cells in MigLayout?

查看:120
本文介绍了如何消除MigLayout中两个单元格之间的垂直间隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题:如何删除包含两个JCheckBox的两个单元格之间的垂直间隙?我用红色边框标记了图片中的空白.

Very simple question: How can I remove the vertical gap between the two cells containing the two JCheckBox? I have marked the gap in the picture with a red border.

这是代码:

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class Main {
    public static void main(String[] args) {
        JPanel somePanel = new JPanel();
        somePanel.setLayout(new MigLayout("insets 0, debug", "", ""));
        somePanel.add(new JCheckBox("first option"), "h 20!");
        somePanel.add(new JButton("click me"), "spany 2, h 40!, w 60%, wrap");
        somePanel.add(new JCheckBox("option two"), "h 20!");

        JFrame frame = new JFrame();
        frame.setContentPane(somePanel);
        frame.pack();
        frame.setVisible(true);
    }
}

推荐答案

如果仅在特定的行/列之间应用最小间距,则在行/列约束中定义最小间距:

Minimum gaps are defined in either in the row/column constraints if they should be applied between particular rows/columns only:

new MigLayout("insets 0, debug", "", "[]0[]"));

(想知道这对您不起作用吗?在这里还可以吗:)

(wondering a bit that this didn't work for you? It's fine here :)

或在layoutContraints中(如果应将它们应用于所有行之间):

or in the layoutContraints if they should be applied between all rows:

new MigLayout("insets 0, gapy 0, debug"));

顺便说一句:布局编码"应遵循与所有编码f.i.相同的规则. DRY :-)特别地,我的规则是如果可以通过布局/行约束实现目标,则不要重复组件约束.在示例中,您可以摆脱除以下范围之外的所有组件约束:

BTW: layout "coding" should follow the same rules as all coding, f.i. DRY :-) In particular, my rule is to not repeat component constraints if you can achieve the goal with layout/row constraints. In the example you can get rid of all component constraints except the spanning by:

somePanel.setLayout(new MigLayout("insets 0, debug, wrap 2", 
        "[][60%, fill]", "[20!, fill]0"));
somePanel.add(new JCheckBox("first option"));
somePanel.add(new JButton("click me"), "spany 2");
somePanel.add(new JCheckBox("option two"));

这篇关于如何消除MigLayout中两个单元格之间的垂直间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆