【问题标题】:Miglayout button overflows constraintsMiglayout 按钮溢出约束
【发布时间】:2012-11-13 23:33:54
【问题描述】:

我正在使用 Miglayout 为我的一个面板创建类似表格的布局。我需要所有面板的固定宽度为 200 像素。当我在面板中添加组件时,一切正常,但是当我尝试插入一个具有长文本的按钮(因此需要超过 200 像素的空间来显示)时,按钮会溢出其单元格并与相邻按钮重叠。这段代码应该能说明我的问题:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPanel content = new JPanel(new MigLayout("wrap 5","[200!]","[50!]"));
        JButton b = new JButton("Button 1");
        content.add(b,"growx");
        b = new JButton("Button 2");
        content.add(b,"growx");
        b = new JButton("Button with a very long text which should not be visible");
        content.add(b,"growx");
        b = new JButton("Button 4");
        content.add(b,"growx");
        b = new JButton("Button 5");
        content.add(b,"growx");
        b = new JButton("Button 6");
        content.add(b,"growx");
        b = new JButton("Button 7");
        content.add(b,"growx");
        b = new JButton("Button 8");
        content.add(b,"growx");
        b = new JButton("Button 9");
        content.add(b,"growx");
        b = new JButton("Button 10");
        content.add(b,"growx");
        f.setContentPane(content);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

我想要的是按钮来显示它可以容纳在 200 像素中的所有文本,然后可能是一些尾随句点,例如“带有版本的按钮...”

有人知道如何实现这一目标吗?

(可以从here获取miglayout进行测试)

【问题讨论】:

    标签: java layout miglayout


    【解决方案1】:

    刚刚下载了布局来看看。你的解决方案只是:

        b = new JButton("Button with a very long text which should not be visible");
        content.add(b,"growx, wmax 200");
    

    它对我有用。

    【讨论】:

    • 好吧,这很奇怪......布局构造函数中的 [200!] 应该这样做......我担心即使你的解决方案有效,它也无济于事因为我只需要在代码中的一个地方指定最大宽度(原因很难解释)。但是 +1 的答案(虽然还没有,因为我希望在未投票列表中获得更多曝光,但很快:)
    • 我不认为列约束应该与某个组件的宽度约束相同。快速入门指南说“未指定的大小将默认为 组件的相应大小。”它没有说明列宽。请记住,没有违反列的最大宽度,因为无论上一列中组件的大小如何,下一列都从相同的偏移量开始。
    • 确实...有什么方法可以在布局构造函数中全局强制组件的最大宽度?我通过添加 wmax 约束暂时“修复”了我的代码,但它很难看,而且不能真正扩展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2015-10-07
    • 2014-11-02
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多