为什么这个组件没有对齐(MigLayout)

时间:2014-06-25 17:46:03

标签: java swing miglayout

我正在努力让这个组件正确对齐。

此SSCCE:

public static void main(String[] args) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
    JFrame frame = new JFrame();
    frame.setLayout(new MigLayout(new LC().fill()));
    final JList<String> company_list = new JList<>();
    company_list.setListData(new String[] {"Company 1", "Company 2", "Company 3", "Company 4", "Company 5"});
    JScrollPane company_list_jsp = new JScrollPane(company_list);
    frame.add(company_list_jsp, "dock west, width 150px, growx");
    final MainPanel mainPanel = new MainPanel();
    frame.add(mainPanel, "grow");
    company_list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            mainPanel.setCompanyName(company_list.getSelectedValue());
        }
    });
    frame.setSize(800,600);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

static class MainPanel extends JPanel {

    JLabel company_name_label;
    JList company_contacts_list;
    JList customers_list;
    JTextArea company_notes_textArea;
    public MainPanel() {
        setLayout(new MigLayout(new LC().fillX()));
        company_name_label = new JLabel();
        company_name_label.setText("Company");
        company_name_label.setFont(new Font("Tahoma", Font.PLAIN, 24));
        add(company_name_label, "cell 0 0 1 3, span");

        JPanel panel1 = new JPanel(new MigLayout(new LC().fill().insetsAll("0")));
        JLabel label1 = new JLabel("Company Contacts");
        panel1.add(label1, "cell 0 1 1 1, sg 1");
        JLabel label2 = new JLabel("Customers");
        panel1.add(label2, "cell 1 1 1 1, sg 1");
        JLabel label3 = new JLabel("Company Notes");
        panel1.add(label3, "cell 2 1 1 1, sg 1");

        company_contacts_list = new JList();
        JScrollPane company_contacts_list_jsp = new JScrollPane(company_contacts_list);
        panel1.add(company_contacts_list_jsp, "cell 0 2 1 1, height 150px, growx, sg 2");

        customers_list = new JList();
        JScrollPane customers_list_jsp = new JScrollPane(customers_list);
        panel1.add(customers_list_jsp, "cell 1 2 1 1, height 150px, growx, sg 2");

        company_notes_textArea = new JTextArea();
        company_notes_textArea.setColumns(20);
        company_notes_textArea.setRows(5);
        company_notes_textArea.setWrapStyleWord(true);
        company_notes_textArea.setLineWrap(true);
        JScrollPane company_notes_textArea_jsp = new JScrollPane(company_notes_textArea);
        panel1.add(company_notes_textArea_jsp, "cell 2 2 1 1, height 150px, growx, sg 2");

        add(panel1, "span, grow");
    }

    public void setCompanyName(String companyName) {
        company_name_label.setText(companyName);
    }
}

产生这个:

enter image description here

JTextArea未与其他两个组件正确对齐的地方。

注意:如果您注释掉Windows LookAndFeel代码,则可以使用:

enter image description here

但是改变申请的LAF不是一种选择。

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

您可以通过将rowConstraints的{​​{1}}参数设置为MigLayout来指定整个第3行垂直填充。例如:

"[][][fill]"

MigLayout with the third row vertically filling