gpt4 book ai didi

java - 如何在 boxLayout 的两个按钮之间添加空格?

转载 作者:IT老高 更新时间:2023-10-28 20:42:29 25 4
gpt4 key购买 nike

我在 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 类创建不可见的组件。

由于您已经使用过胶水但没有成功(我怀疑为什么?),您可以尝试刚性区域

// 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 ,

三个因素会影响容器中可见组件之间的空间量:

  • 布局管理器

    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 How to Use BoxLayout 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 How to Use Borders .

关于java - 如何在 boxLayout 的两个按钮之间添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com