我有以下代码:
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);
}
产生这个:
但是,我正在尝试使
JTextFields
均匀分布。所以,我改变了:
JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));
至
JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));
(fill()与组合fillX()和fillY()的工作原理相同,它会产生:
但是,我不希望
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);
}
关于java - MigLayout LC::fill()无需调整组件大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23188597/