简体   繁体   English

使用miglayout设置宽度

[英]Setting width with miglayout

I'm trying to lay out components and want to set the width in a specific way. 我正在尝试布局组件,并希望以特定方式设置宽度。 From what i understand, miglayout sets width like "width min:pref:max". 根据我的理解,miglayout设置宽度,如“width min:pref:max”。 So in my case i want the following: 所以在我的情况下,我想要以下内容:

布局解释

My problem is with comp2. 我的问题是comp2。 It stops growing after about 200px and I can't figure out why, since I don't specify a maximum width. 它在大约200px后停止增长,我无法弄清楚为什么,因为我没有指定最大宽度。

I also checked the miglayout swing demo but they don't have my exact case there. 我还检查了miglayout swing演示,但他们没有我的确切案例。 They have one with unlimited width, but not minimum width specified. 它们的宽度不受限制,但不是指定的最小宽度。

To make sure the panel expands, I set the background of the panel to gray, and I can see that it expands without any problem. 为了确保面板扩展,我将面板的背景设置为灰色,我可以看到它扩展没有任何问题。

Please tell me if anything needs to be clarified and I'll gladly try to explain better. 请告诉我是否需要澄清任何事情,我很乐意尝试更好地解释。

EDIT: Here is a SSCCE 编辑:这是一个SSCCE

import java.awt.Color;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class SSCCE {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new MigLayout(
                "",
                "[grow, fill]",
                ""));
        panel.setBackground(Color.LIGHT_GRAY);

        JButton comp1 = new JButton("Comp1");
        JButton comp2 = new JButton("Comp2");
        JButton comp3 = new JButton("Comp3");

        panel.add(comp1, "width 50:150:150, growx");
        panel.add(comp2, "growx");
        panel.add(comp3, "width 50:70:70, growx");      

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);     
    }
}

I does grow, but i want it to take all the space available, and it does not. 我确实在成长,但我希望它能占用所有可用空间,但事实并非如此。 Can it have something to do with the settings on the panel? 它可以与面板上的设置有关吗?

EDIT 2: With this code: 编辑2:使用此代码:

panel.add(comp1, "width 50:150:150");
panel.add(comp2, "width 10:n:n");
panel.add(comp3, "width 50:70:70, right");  

I get this: 我明白了:

在此输入图像描述

But i want the middle component to occupy all available space. 但我希望中间组件占用所有可用空间。

Feeling free :-) 感觉自由:-)

Here's a snippet that gives all extra space to the middle column. 这是一个片段,为中间列提供了所有额外的空间。

JPanel panel = new JPanel(new MigLayout(
        "debug",
        "[][grow, fill][]",
        ""));
JButton comp1 = new JButton("Comp1");
JButton comp2 = new JButton("Comp2");
JButton comp3 = new JButton("Comp3");

panel.add(comp1, "width 50:150:150");
panel.add(comp2); 
panel.add(comp3, "width 50:70:70");      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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