How do I stop center of BorderLayout from taking up east and west?

醉酒当歌 提交于 2019-12-24 15:13:53

问题


I have a program in which I use BorderLayout and the EAST and WEST positions are not filled, though the CENTER one is. As a result, the center component extends to the space where the east and west components would be.

How can I prevent this? I know I could just use a blank JPanel in the east and west positions, but that seems hacky.


回答1:


If what you want is empty space around your center panel, you can add a border to it:

panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10);



回答2:


You said:

I know I could just use a blank JPanel in the east and west positions, but that seems hacky.

Why is that hacky? How else would you control the amount of size of the visible space? What if you only want it to be a narrow (say, 10 pixels) blank space? What if you want it to be 100 pixels? What if you want the border to grow and shrink based on the size of the corresponding frame?

Adding some sort of component there (it doesn't have to be a JPanel, though that's not a bad choice) allows you to specify the size of it more effectively, and there's nothing inherently wrong with doing so.




回答3:


What you need is Box.Filler. It is used to create empty components to fill areas. See https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler



来源:https://stackoverflow.com/questions/32122838/how-do-i-stop-center-of-borderlayout-from-taking-up-east-and-west

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!