[英]MigLayout LC::fill() without resizing components
I have this code: 我有以下代码:
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);
}
which produces this: 产生这个:
But, I am trying to get the JTextFields
to space out evenly. 但是,我正在尝试使
JTextFields
均匀间隔。
So, I change: 所以,我改变了:
JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));
to 至
JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));
(fill() works the same as combining fillX() and fillY()) which produces: (fill()的工作原理与将fillX()和fillY()组合在一起的结果相同:
However, I do not wish for the JTextFields
to resize, only the gaps between them to increase. 但是,我不希望
JTextFields
调整大小,只是希望它们之间的间隙增加。 Is there a way to accomplish this with MigLayout? 有没有办法用MigLayout做到这一点?
I figured it out. 我想到了。 It is because I was using the
grow
attribute for each component. 这是因为我为每个组件使用了
grow
属性。 The proper attribute to use is growx
. 正确使用的属性是
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.