【发布时间】:2022-07-24 16:16:14
【问题描述】:
我想按此图所示布局我的组件。
简而言之:
- aTextField 必须有 250px 的固定大小;
- 按钮具有固定大小,取决于文本标签;
- TextField 应该增长,使其宽度加上按钮宽度达到 250 像素。
这是我的代码:
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();
}
}
不幸的是,MigLayout 似乎不允许根据您通过 id 回忆的其他组件来计算宽度。 运行我的代码时,我得到:
Caused by: java.lang.IllegalArgumentException: Size may not contain links
我错过了什么吗?除了通过 id 引用组件之外,我怎样才能达到预期的结果?