I have created n JPanels and in each JPanel I have added 3 components. I added these JPanels to a new JPanel as rows. The layout for the n JPanels is FlowLayout and for the main panel is BorderLayout. The setPrefferedSize() method is working fine for the components which I have added in the n JPanels but it is not working f开发者_运维百科or the n JPanels.
I am trying npanels[i].setPrefferedSize(new Dimension(300,25)), I want the height of the JPanel to be equal to height the components added in it (which is 25). Is there any constraint that the height of a JPanel should be some minimum value? Please help I tried a lot of things but it's not working.....
Some layout managers tend to ignore the size setting...
Read somewhere that BorderLayout might tend to ignore the width
for NORTH
and SOUTH
components,
height
for EAST
and WEST
,
both height
and width
are ignored for CENTER
...
Could this be the case?
Also, can you provide a screenshot or a diagram explaining whats happening?
The setPrefferedSize() method is working fine for the components
There is generally no need to set a preferred size for components. Swing will calculate the preferred size automatically.
layout for n JPanels is FlowLayout... which i have added in n JPanels but it is not working for n JPanels
Again, there is no need to set the preferred size of each panel. The size will be calculated automatically based on the preferred size of all the components.
the main panel is BorderLayout
This does not make sense since you can't add "n" panels to the BorderLayout. You can only add one component to the North,Center and South so you can have a maximum of 3 different vertically display panels. In this case if you use frame.pack() then each panel will be displayed at its preferred size. On the other hand if you use frame.setSize(300, 400) then the hieght of the Center panel will be stretched.
Since it appears you want all panels the same size maybe you should be using a GridLayout, otherwise you can try a BoxLayout. Read the Swing tutorial. It explains all about using the layout managers.
If you need more help post your SSCCE.
精彩评论