Не допускайте появления компонентов MigLayout - PullRequest
0 голосов
/ 07 июня 2018

У меня есть 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 '

Как бы вы это сделали?

1 Ответ

0 голосов
/ 07 июня 2018

Согласно Краткое руководство по MigLayout вам необходимо разделить и охватить ваших ячеек:

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

Объяснение: РазделениеПервая ячейка означает, что следующие два компонента добавляются в такую ​​же ячейку.Это означает, что в строке по-прежнему осталось три ячейки, поэтому вы должны охватить текстовое поле этими ячейками.

...