保持MigLayout组件的间距

时间:2018-06-07 06:24:52

标签: 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");

此处的目标是为除growx 0之外的所有组件JTextField说明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");

Nothin doin&#39;

你会怎么做?

1 个答案:

答案 0 :(得分:1)

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

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

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