You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

问题

  1. 请问我是否遗漏了什么配置导致报错?
  2. 除了通过ID引用组件宽度的方式外,还有什么方法可以实现该布局需求?

内容的提问来源于stack exchange,提问作者BabaNew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 18:30:54