gpt4 book ai didi

java - 为什么这个组件不对齐(MigLayout)

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:02 25 4
gpt4 key购买 nike

我在尝试让这个组件正确对齐时遇到了最令人沮丧的时间。

本次 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 不是一个选项。

有谁知道为什么会这样吗?

最佳答案

您可以通过将 MigLayoutrowConstraints 参数设置为 “[][][fill]” 来指定整个第三行垂直填充。例如:

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

MigLayout with the third row vertically filling

关于java - 为什么这个组件不对齐(MigLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24415275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com