2012-04-02 32 views
0

是否有我能實現的方法來確保窗格中的每個按鈕都保持最小高度?我試圖使用itembutton.setSize()方法,但它沒有效果。爲JScrollPane中的每個組件設置最小高度

JPanel itemPanel = new JPanel(); 

itemPanel.setLayout(new GridLayout(0,1)); 

for(final Item i: list){ 
    JButton itemButton = new JButton(i.getName()); 
    itemPanel.add(itemButton); 
} 

JScrollPane itemPane = new JScrollPane(itemPanel); 

回答

0

itembutton.setMinimumSize(minimumSize)

編輯:剛發現,因爲this java tutorial似乎告訴,用GridLayout沒有辦法做到這一點。

每個組件需要的所有可用空間的細胞內,且每個單元是完全一樣的大小

所以我猜你必須嘗試另一種佈局。我可以建議(不知道它是否合適,但它的工作原理)GridBagLayout。例如,2個按鈕:

itemPanel.setLayout(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
c.fill = GridBagConstraints.HORIZONTAL; 
c.gridx = 0; 
c.weightx = 0.5; 
itemPanel.add(new JButton("A"), c); 
c.gridx = 1; 
c.weightx = 0.5; 
itemPanel.add(new JButton("B"), c); 

,看一下http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

+0

注意,我相信(不知道這一點),這也取決於所選擇的佈局和他談論大小的提示。 – user978548 2012-04-02 02:14:48

+0

是的,我認爲問題來自我選擇的佈局之一。無論如何謝謝你的幫助 – rmp2150 2012-04-02 02:19:45

+0

所以setMinimumSize()沒有效果? – user978548 2012-04-02 02:21:27

相關問題