如何在boxLayout中的两个按钮之间添加空格? [英] How can I add a space in between two buttons in a boxLayout?

查看:96
本文介绍了如何在boxLayout中的两个按钮之间添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 BoxLayout 组中有四个按钮。这只是两个样本,因为它都是重复的代码。我想在每个按钮之间创建一个微小的空间,这样它们就不会相互碰撞。我几乎尝试了 .add(Box.Create ....)中的所有方法,但没有任何效果。

I have four buttons in a BoxLayout group. This is just a sample of two because it's all repeated code. I want to create a slight space between each button so they don't run into each other. I have tried practically every method in the .add(Box.Create....) and nothing worked.

    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类的帮助下创建不可见的组件。

If you want to have space between components, you can either add an empty border to one or both components, or insert invisible components to provide the space. You can create invisible components with the help of the Box class.

既然你已经使用了胶水没有成功(我怀疑为什么?),你可以尝试类似刚性区域,

since you already used glue with no success (I doubt why?), you may try something like Rigid area,

container.add(firstComponent);
container.add(Box.createRigidArea(new Dimension(5,0)));
container.add(secondComponent); 

看看使用Invisible Components作为填充程序,它为您提供了很多选项和解释。

Have a look at Using Invisible Components as Filler which gives you a lot of options and explanations.

附加信息,来自在组件之间放置空间

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

Three factors influence the amount of space between visible components in a container:


  • 布局管理器

  • The layout manager


一些布局管理器会自动在组件之间放置空间;别人不这样做。有些允许您指定组件之间的空间量。有关间距支持的信息,请参阅每个布局管理器的操作方法页面。

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.


  • 不可见组件

  • Invisible components


    您可以创建不执行绘制的轻量级组件,但这会占用GUI中的空间。通常,您在由 BoxLayout 控制的容器中使用不可见组件。有关使用隐形组件的示例,请参见如何使用BoxLayout

    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.


  • 空边框

  • Empty borders


    否无论布局管理器是什么,您都可以通过向组件添加空边框来影响组件之间的明显空间量。空边框的最佳候选者是通常没有默认边框的组件,例如面板和标签。由于绘制代码的实现方式,某些其他组件在某些外观实现中可能无法很好地处理边框。有关边框的信息,请参阅如何使用边框


  • 这篇关于如何在boxLayout中的两个按钮之间添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆