2012-04-02 31 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

相关问题