如果要在组件之间具有空格,可以向一个或两个组件添加空边框,或插入不可见组件以提供空间。您可以在Box类的帮助下创建不可见组件。
因为你已经使用胶水没有成功(我怀疑为什么?),你可以尝试像刚性区域,
container.add(firstComponent);
container.add(Box.createRigidArea(new Dimension(5,0)));
container.add(secondComponent);
三个因素影响容器中可见组件之间的空间量:
>布局管理器
Some layout managers automatically put space between components; others do not. Some let you specify the amount of space between components. See the how-to page for each layout manager for information about spacing support.
>隐形部件
You can create lightweight components that perform no painting, but that can take up space in the GUI. Often, you use invisible components in containers controlled by BoxLayout. See 07002 for examples of using invisible components.
>空边框
No matter what the layout manager, you can affect the apparent amount of space between components by adding empty borders to components. The best candidates for empty borders are components that typically have no default border, such as panels and labels. Some other components might not work well with borders in some look-and-feel implementations, because of the way their painting code is implemented. For information about borders, see 07003 .