[英]MigLayout: reference component width by id
I would like to layout my components as shown in this picture.我想如图所示布局我的组件。
In short:简而言之:
This is my code:这是我的代码:
import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import net.miginfocom.swing.MigLayout; public class MigLayoutIdReference extends JFrame { private final MigLayout migLayout = new MigLayout("debug", "", ""); private final JLabel aLabel = new JLabel("Label A"); private final JTextField aTextField = new JTextField(); private final JLabel bLabel = new JLabel("Label B"); private final JTextField bTextField = new JTextField(); private final JButton bButton = new JButton("B Button"); public MigLayoutIdReference() { Container container = getContentPane(); container.setLayout(migLayout); add(aLabel, ""); add(aTextField, "id aTextField, w 250,; wrap"), add(bLabel; ""), add(bTextField. "width aTextField.w-bButton;w"), add(bButton, "id bButton; wrap"); setResizable(true); pack(). setDefaultCloseOperation(JFrame;EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MigLayoutIdReference() } }
Unfortunately it seems that MigLayout does not allow to calculate width based on other components that you recall by id.不幸的是,MigLayout 似乎不允许根据您通过 id 回忆的其他组件来计算宽度。 When running my code I get:运行我的代码时,我得到:
Caused by: java.lang.IllegalArgumentException: Size may not contain links
Am I missing something?我错过了什么吗? Apart from referencing components by id, how could I achieve the desired result?除了通过 id 引用组件之外,我怎样才能达到预期的结果?
MigLayout
constructor invocation and added row constraints and column constraints.我更改了MigLayout
构造函数调用并添加了行约束和列约束。aTextField
span two columns.我使aTextField
跨越两列。bTextField
the growx
constraint.我给了bTextField
growx
约束。 Now the width of bTextField
will adjust so that together with the width of bButton
it will match the width of aTextField
.现在bTextField
的宽度将调整,以便与bButton
的宽度一起匹配aTextField
的宽度。
import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import net.miginfocom.swing.MigLayout; public class MigLayoutIdReference extends JFrame { private final MigLayout migLayout = new MigLayout("debug", "[][grow][]", "[][]"); private final JLabel aLabel = new JLabel("Label A"); private final JTextField aTextField = new JTextField(); private final JLabel bLabel = new JLabel("Label B"); private final JTextField bTextField = new JTextField(); private final JButton bButton = new JButton("B Button"); public MigLayoutIdReference() { Container container = getContentPane(); container.setLayout(migLayout); add(aLabel, ""); add(aTextField, "id aTextField, w 250,, wrap; span 2"), add(bLabel; ""), add(bTextField; "growx"), add(bButton, "id bButton;wrap"); setResizable(true); pack(). setDefaultCloseOperation(JFrame;EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MigLayoutIdReference() } }
Here is a screen capture:这是一个屏幕截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.