3

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

差距

这是代码:

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

3 回答 3

3

如果它们应该仅应用于特定的行/列之间,则在行/列约束中定义最小间隙:

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

(有点想知道这对你没用?这里很好:)

或者在 layoutContraints 中是否应该在所有行之间应用它们:

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

顺便说一句:布局“编码”应该遵循与所有编码相同的规则,fi DRY :-) 特别是,如果您可以通过布局/行约束实现目标,我的规则是不要重复组件约束。在示例中,您可以摆脱除跨越之外的所有组件约束:

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"));
于 2012-10-30T10:34:23.023 回答
2

好的,我刚刚找到了一个使用对接的好解决方案:

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 JButton("click me"), "east");
        somePanel.add(new JCheckBox("first option"), "north");
        somePanel.add(new JCheckBox("option two"), "south");

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

但是,如果不能选择对接,我该怎么做?

于 2012-10-29T16:37:55.137 回答
0

另一种解决方案是将单元格拆分为两个子单元格,将复选框放在那里,然后应用组件间隙约束。

package com.zetcode;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

/*
Demonstrating component gaps in MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutGapsEx extends JFrame {

    public MigLayoutGapsEx() {

        initUI();
    }

    private void initUI() {

        JCheckBox cb1 = new JCheckBox("First option");
        JCheckBox cb2 = new JCheckBox("Second option");

        JButton btn = new JButton("Click me");

        createLayout(cb1, cb2, btn);

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout());

        add(arg[0], "split 2, flowy");
        add(arg[1], "gapy 0");
        add(arg[2]);

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutGapsEx ex = new MigLayoutGapsEx();
            ex.setVisible(true);
        });
    }
}

这是屏幕截图:

示例截图

于 2016-08-25T15:44:50.667 回答