java - 防止 MigLayout 组件间隔开

标签 java miglayout

我有这个 SSCCE:

public class FiltersPanel extends JPanel {

    public FiltersPanel() {
        init();
    }

    class Filter {
        String name;
        FilterType type;

        public Filter(String name, FilterType type) {
            this.name = name;
            this.type = type;
        }
    }

    enum FilterType {
        Money("$"),
        Percent("%"),
        Integer("#");
        String sign;

        private FilterType(String sign) {
            this.sign = sign;
        }
    }

    LinkedHashSet<Filter> filters;

    void addFilter(Filter filter) {

        JCheckBox checkBox_min = new JCheckBox(filter.name + " Min (" + filter.type.sign + ")");
        JTextField textField_min = new JTextField();
        JCheckBox checkBox_max = new JCheckBox(filter.name + " Max (" + filter.type.sign + ")");
        JTextField textField_max = new JTextField();
        add(checkBox_min, "growx");
        add(textField_min, "growx");
        add(checkBox_max, "growx");
        add(textField_max, "growx, span");
    }

    void init() {
        setLayout(new MigLayout(new LC().fill()));

        JCheckBox date_min_checkBox = new JCheckBox("Date Min:");
        JDateChooser date_min_dateChooser = new JDateChooser();
        JCheckBox date_max_checkBox = new JCheckBox("Date Max:");
        JDateChooser date_max_dateChooser = new JDateChooser();

        int row = 0;
        add(date_min_checkBox, "growx");
        add(date_min_dateChooser, "growx");
        add(date_max_checkBox, "growx");
        add(date_max_dateChooser, "growx, span");

        addFilter(new Filter("Sales Amt", FilterType.Money));
        addFilter(new Filter("Sales Qty", FilterType.Integer));
        addFilter(new Filter("SO Count", FilterType.Integer));

        addFilter(new Filter("Quotes Amt", FilterType.Money));
        addFilter(new Filter("Quotes Qty", FilterType.Integer));
        addFilter(new Filter("CQ Count", FilterType.Integer));

        addFilter(new Filter("Profit Margin", FilterType.Percent));
        addFilter(new Filter("Profit Total", FilterType.Money));
        addFilter(new Filter("Price", FilterType.Money));
        addFilter(new Filter("STQ Ratio", FilterType.Percent));

        addFilter(new Filter("QTY", FilterType.Integer));
        addFilter(new Filter("Inv Cost", FilterType.Money));


        JCheckBox pn_checkBox = new JCheckBox("Part #");
        pn_checkBox.setBorder(new LineBorder(Color.yellow));
        pn_checkBox.setBorderPainted(true);
        JRadioButton startsWith_button = new JRadioButton("Starts With");
        startsWith_button.setBorder(new LineBorder(Color.yellow));
        startsWith_button.setBorderPainted(true);
        JRadioButton contains_button = new JRadioButton("Contains");
        contains_button.setBorder(new LineBorder(Color.yellow));
        contains_button.setBorderPainted(true);
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(startsWith_button);
        buttonGroup.add(contains_button);
        JTextField pn_textField = new JTextField();
        pn_textField.setBorder(new LineBorder(Color.yellow));
        add(pn_checkBox, "newline, growx 0");
        add(startsWith_button, "growx 0");
        add(contains_button, "growx 0");
        add(pn_textField, "spanx, growx 100");


        JCheckBox conditions_checkBox = new JCheckBox("Conditions:");
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame();
        frame.add(new FiltersPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这会产生这个:

enter image description here

我试图让组件呈黄色,以便它们之间没有间隙,并使最后一个 JTextField 跨越行的其余部分。

这是我关注的代码:

add(pn_checkBox, "newline, growx 0");
add(startsWith_button, "growx 0");
add(contains_button, "growx 0");
add(pn_textField, "spanx, growx 100");

此处的目标是为除 JTextField 之外的所有组件指定 growx 0(即 growx 100)。

也尝试过这个:

add(pn_checkBox, "newline, align left, push, growx 0");
add(startsWith_button, "align left, push, growx 0");
add(contains_button, "align left, push, growx 0");
add(pn_textField, "spanx, growx 100");

没事做

你会怎么做?

最佳答案

根据 MigLayout Quick Start Guide您必须分割跨越您的单元格:

add(pn_checkBox, "newline, split 3");
add(startsWith_button);
add(contains_button);
add(pn_textField, "span 3, grow");

说明:拆分第一个单元格意味着接下来的两个组件将添加到同一单元格中。这意味着该行中还剩下三个单元格,因此您必须将文本字段跨越这些单元格。

关于java - 防止 MigLayout 组件间隔开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734237/

相关文章:

java - Spring Data Repositories - 查找列表中的 where 字段

java - 使用 MigLayout,为什么 JTable 后面的 JButton 无响应以及如何解决此问题?

Java MigLayout 在行中固定等宽

java - 并发访问实用程序静态方法

java - FTPClient下载文件失败,retrieveFile()方法replyCode=550

java - 插入二叉搜索树的两种相似算法

java - 请给出一个简单的例子,如何使用 MigLayout 模拟 BorderLayout?

java - MiGLayout 会包含在 Java 7 中吗?

java - 如何使用JavaBuilders创建JFreeChart控件?

java - 在编译时或运行时编程