MigLayout通过ID引用组件宽度报错,如何实现指定布局需求?
MigLayout布局问题:实现固定宽度与自适应组件组合
布局需求
我需要实现如图所示的组件布局:
具体要求:
aTextField固定宽度为250px;bButton宽度由自身文本标签决定;bTextField自适应宽度,使其与bButton的宽度之和等于250px。
我的实现代码
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(); } }
报错信息
Caused by: java.lang.IllegalArgumentException: Size may not contain links
问题
- 请问我是否遗漏了什么配置导致报错?
- 除了通过ID引用组件宽度的方式外,还有什么方法可以实现该布局需求?
内容的提问来源于stack exchange,提问作者BabaNew
相关产品推荐
相关产品推荐

