【问题标题】:MigLayout: reference component width by idMigLayout:通过 id 参考组件宽度
【发布时间】: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 引用组件之外,我怎样才能达到预期的结果?

    标签: java swing miglayout


    【解决方案1】:
    • 我更改了MigLayout 构造函数调用并添加了行约束和列约束。
    • 我让aTextField 跨越两列。
    • 我给了bTextField growx 约束。

    现在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();
        }
    }
    

    这是一个屏幕截图:

    【讨论】:

    • 谢谢,你做到了。无论如何,你知道一些关于使用组件 id 来实现这一点的知识吗?
    猜你喜欢
    • 2012-09-14
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2019-09-02
    • 2011-10-14
    • 2014-02-09
    相关资源
    最近更新 更多