Why setPreferredSize does not change the size of the button?

此生再无相见时 提交于 2019-12-01 20:57:41

Layout managers are free to ignore the preferred size. Specifically, GridLayout will always make each cell in the grid exactly the same size (it's a pretty useless layout manager for that reason).

You'll have to use a different layout manager, such as nested BoxLayout or a GroupLayout.

GridLayout is quite inflexible in that each and every cell is the same size, typically honoring the largest height and width settings of any object added to the grid.

If the rows and/or columns need to have varying sizes you should use GridBagLayout.

Apurva Mayank

setPreferredSize will not change the size of the button until dimension is set by using Dimension.

Example:-

Dimension dim = new Dimension(20,20), then use setPerferredSize(dim).

marcello

I found this method version, that fits perfect to your solution:

.addComponent(<<a jButton instance>>, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, <<put here an integer to set you max width button size>>)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!