【问题标题】:How to remove vertical gap between two cells in MigLayout?如何消除 MigLayout 中两个单元格之间的垂直间隙?
【发布时间】:2012-10-19 00:18:52
【问题描述】:

非常简单的问题:如何消除包含两个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);
    }
}

【问题讨论】:

    标签: java swing layout miglayout gaps-in-visuals


    【解决方案1】:

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

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

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

    或者如果它们应该应用于所有行之间,则在 layoutContraints 中:

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

    顺便说一句:布局“编码”应遵循与所有编码相同的规则,f.i. 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"));
    

    【讨论】:

    • 非常感谢这个答案,这澄清了很多。到目前为止,我从未使用过行/列约束。看来我一定要调查一下。 :)
    【解决方案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);
        }
    }
    

    但如果不能停靠,我该怎么做呢?

    【讨论】:

    • 移除(布局的)行约束中的间隙
    • 我发现 @mKorbel MigLayout 非常好。至少对我来说。 ;)
    • @kleopatra 你会怎么做?我在布局本身的行配置中尝试了gap 0gapbottom 0/gaptop 0gap 0。没有任何效果...
    • @kleopatra 啊,我必须将gap 0 放入布局约束。 :) 你能把它放在一个答案中以便我接受吗?
    【解决方案3】:

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

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

    截图如下:

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多