2010-06-28 61 views
0

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

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用于测试)

回答

2

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

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

它为我工作。

+0

确定这是奇怪的......在布局构造函数中的[200!]应该这样做...即使您的解决方案工作,但我害怕,但它并不能帮助我,因为我需要指定仅在代码中的最大宽度(原因很复杂)。但答案+1(还没有,因为我想在没有upvoted列表更多的曝光,但很快:) – 2010-06-28 14:53:19

+0

我不认为列约束应该是相同的宽度约束的某个组件。快速入门指南中提到“未指定大小将默认为**组件的** 相应大小。”它没有提及柱宽。请记住,列的最大宽度并未违反,因为下一列始于相同的偏移量,无论前一列中的组件大小如何。 – 2010-06-28 15:12:37

+0

确实...任何方式来强制布局构造函数的全局组件的最大宽度虽然?我暂时通过添加wmax约束来“修复”我的代码,但它很丑并且不能真正扩展。 – 2010-06-28 15:20:29