2010-06-28 63 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