Miglayout按钮溢出约束

时间:2010-06-28 14:07:08

标签: java layout miglayout

我正在使用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像素的所有文本,然后可能是一些尾随时期,如“按钮与ver ...”

有没有人知道如何实现这个目标?

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

1 个答案:

答案 0 :(得分:2)

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

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

它对我有用。