代码之家  ›  专栏  ›  技术社区  ›  Savvas Dalkitsis

布局按钮溢出约束

  •  0
  • Savvas Dalkitsis  · 技术社区  · 16 年前

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

    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);
        }
    
    }
    

    有人知道如何做到这一点吗?

    here 用于测试)

    1 回复  |  直到 16 年前
        1
  •  2
  •   Mark Peters    16 年前

    只是下载了布局来检查一下。您的解决方案是否只是:

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