简体   繁体   English

如何阻止BorderLayout的中心向东和向西移动?

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

I have a program in which I use BorderLayout and the EAST and WEST positions are not filled, though the CENTER one is. 我有一个程序,其中我使用BorderLayout ,尽管CENTER位置是一个,但EASTWEST位置未填充。 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. 我知道我可以只在东部和西部使用空白的JPanel ,但这似乎很不客气。

如果您想要的是中央面板周围的空白区域,则可以为其添加边框:

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

You said: 你说:

I know I could just use a blank JPanel in the east and west positions, but that seems hacky. 我知道我可以只在东部和西部使用空白的JPanel ,但这似乎很不客气。

Why is that hacky? 为什么这么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? 如果只希望它是一个狭窄的(例如10像素)空白空间,该怎么办? What if you want it to be 100 pixels? 如果您希望它是100像素怎么办? 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. 在其中添加某种组件(不必是JPanel ,这不是一个坏选择),它使您可以更有效地指定其大小,这样做没有内在的错误。

What you need is Box.Filler . 您需要的是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://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM