我在BoxLayout
组有四个按钮。这只是两个样本,因为它都是重复的代码。我想在每个按钮之间创建一个小空间,这样它们就不会相互碰撞。我几乎尝试了.add(Box.Create....)
中的所有方法,但没有任何效果。
enter.add(Box.createVerticalGlue());
enter.add(Box.createHorizontalGlue());
//enter.add(new JSeparator(SwingConstants.HORIZONTAL));
JButton float = new JButton("LOWER");
float.add(Box.createVerticalGlue());
float.add(Box.createHorizontalGlue());
如果要在组件之间留出空间,可以向一个或两个组件添加空边框,或插入不可见组件以提供空间。您可以在Box类的帮助下创建不可见的组件。
因为你已经使用胶水没有成功(我怀疑为什么?),你可以试试像Rigid area这样的东西,
// Horizontal spacer
container.add(firstComponent);
container.add(Box.createRigidArea(new Dimension(5, 0)));
container.add(secondComponent);
看看Using Invisible Components as Filler,它为您提供了很多选择和解释。
其他信息,来自Putting Space Between Components,
有三个因素影响容器中可见组件之间的空间量:
BoxLayout
控制的容器中使用隐形组件。有关使用不可见组件的示例,请参阅How to Use BoxLayout。