2014-06-25 34 views
1

我有最令人沮丧的时间试图让这个组件正确对齐。为什么这个组件不对齐(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不与其他两个组件正确对齐。

注:如果您注释掉WindowsLookAndFeel代码,它的工作原理:

enter image description here

但改变我的应用程序的LAF是不是一种选择。

有谁知道为什么会发生这种情况?

回答

1

通过将MigLayoutrowConstraints参数设置为"[][][fill]",可以指定整个第3行垂直填充。例如:

JPanel panel1 = new JPanel(new MigLayout("fill,insets 0", "[][][]", "[][][fill]")); 

MigLayout with the third row vertically filling

+0

太好了!谢谢!我已经阅读了快速入门和白皮书多次,但了解'ColumnConstraints'和'RowContraints'对我来说非常困难。 – ryvantage