boxlayout java,BoxLayout

布局管理器,允许垂直或水平布置多个组件。

这些部件不会包裹,因此,例如,当调整框架尺寸时,垂直布置的部件将保持垂直布置。

Example:

39808b0bb2e2e57dc113e4e3d96fe02a.gif

使用水平和垂直的不同组合嵌套多个面板会产生类似于GridBagLayout的效果,而不会产生复杂性。 该图显示了两个水平排列的面板,每个面板包含垂直排列的3个组件。

BoxLayout管理器使用轴参数构造,该参数指定将要完成的布局类型。 有四种选择:

X_AXIS - Components are laid out horizontally from left to right.

Y_AXIS - Components are laid out vertically from top to bottom.

LINE_AXIS - Components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out horizontally, otherwise they are laid out vertically. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left. For vertical orientations components are always laid out from top to bottom.

PAGE_AXIS - Components are laid out the way text lines are laid out on a page, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out vertically, otherwise they are laid out horizontally. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left.  For vertical orientations components are always laid out from top to bottom.

对于所有方向,组件的排列顺序与添加到容器中的顺序相同。

BoxLayout尝试以其首选宽度(水平布局)或高度(垂直布局)排列组件。 对于水平布局,如果不是所有组件都具有相同的高度,BoxLayout会尝试使所有组件与最高组件一样高。 如果对于特定组件不可能,则BoxLayout根据组件的Y对齐垂直对齐该组件。 默认情况下,组件的Y对齐为0.5,这意味着组件的垂直中心应与具有0.5 Y对齐的其他组件的垂直中心具有相同的Y坐标。

类似地,对于垂直布局,BoxLayout尝试使列中的所有组件与最宽的组件一样宽。 如果失败,则根据X对齐水平对齐它们。 对于PAGE_AXIS布局,水平对齐基于组件的前沿完成。 换句话说,如果容器的ComponentOrientation从左到右,则X对齐值0.0表示组件的左边缘,否则表示组件的右边缘。

许多程序使用Box类,而不是直接使用BoxLayout。 Box类是一个使用BoxLayout的轻量级容器。 它还提供了方便的方法来帮助您很好地使用BoxLayout。 将组件添加到多个嵌套框是获得所需排列的有效方法。

有关更多信息和示例,请参阅“Java教程”中的 How to Use BoxLayout部分。

警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始, java.beans软件包中添加了对所有JavaBeans java.beans长期存储的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值