gpt4 book ai didi

java - MigLayout LC::fill() 无需调整组件大小

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

我有这个代码:

public static void main(String[] args) {
JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));
panel1.add(new JTextField("text1"), "span, grow");
panel1.add(new JTextField("another text field"), "span, grow");
panel1.add(new JTextField("text3"), "span, grow");

JPanel panel2 = new JPanel(new MigLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(15);
textArea.setRows(7);
JScrollPane jsp = new JScrollPane(textArea);
panel2.add(jsp, "span, grow");

JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
frame.add(panel1);
frame.add(panel2);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

产生这个:

enter image description here

但是,我试图让 JTextFields 均匀分布。

所以,我改变:

JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));

JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));

(fill() 的工作方式与组合 fillX() 和 fillY()) 相同,会产生:

enter image description here

但是,我不希望 JTextFields 调整大小,只希望它们之间的间隙增加。有没有办法用 MigLayout 来完成这个任务?

最佳答案

我明白了。这是因为我为每个组件使用了 grow 属性。正确使用的属性是 growx

public static void main(String[] args) {
JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));
panel1.add(new JTextField("text1"), "span, growx");
panel1.add(new JTextField("another text field"), "span, growx");
panel1.add(new JTextField("text3"), "span, growx");

JPanel panel2 = new JPanel(new MigLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(15);
textArea.setRows(7);
JScrollPane jsp = new JScrollPane(textArea);
panel2.add(jsp, "span, grow");

JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
frame.add(panel1);
frame.add(panel2);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

enter image description here

关于java - MigLayout LC::fill() 无需调整组件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188597/

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