简体   繁体   English

MigLayout:通过 id 参考组件宽度

[英]MigLayout: reference component width by id

I would like to layout my components as shown in this picture.我想如图所示布局我的组件。

在此处输入图像描述

In short:简而言之:

  • aTextField must have a fixed size of 250px; aTextField 必须有 250px 的固定大小;
  • bButton has a fixed size that dependes on the text label; bButton 具有固定大小,取决于文本 label;
  • bTextField should grow so that it's width plus bButton width reach 250px. bTextField 应该增长,使其宽度加上 bButton 宽度达到 250 像素。

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 引用组件之外,我怎样才能达到预期的结果?

  • I changed the MigLayout constructor invocation and added row constraints and column constraints.我更改了MigLayout构造函数调用并添加了行约束和列约束。
  • I made aTextField span two columns.我使aTextField跨越两列。
  • I gave 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM