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);
}
}
產生以下:
凡JTextArea
不與其他兩個組件正確對齊。
注:如果您註釋掉Windows
LookAndFeel
代碼,它的工作原理:
但改變我的應用程序的LAF是不是一種選擇。
有誰知道爲什麼會發生這種情況?
太好了!謝謝!我已經閱讀了快速入門和白皮書多次,但瞭解'ColumnConstraints'和'RowContraints'對我來說非常困難。 – ryvantage