Swing —— Box容器组件

本文介绍如何使用Swing中的Box布局管理器,通过Glue、Strut、Rigid和Filler等组件实现灵活的界面布局调整。这些技巧有助于开发者创建响应式用户界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    普通
      Glue
        Strut
          Rigid
            Filler

               

              普通


              box.add(new JButton("按钮1"));

              box.add(new JButton("按钮2"));

              box.add(new JButton("按钮3"));

               

              Glue

              Glue插入在两组件之间时,它会将两组件挤到最左与最右(或最上与最下),不可见的glue将会占满整个中间的空间。


              box.add(new JButton("按钮1"));

              box.add(Box.createHorizontalGlue());

              box.add(new JButton("按钮2"));

              box.add(Box.createHorizontalGlue());

              box.add(new JButton("按钮3"));

               

              Strut

              当不想让“按钮3”自动紧靠右边,可以使用Strut组件来设置所需要的大小。


               

              box.add(new JButton("按钮1"));

              box.add(Box.createHorizontalGlue());

              box.add(new JButton("按钮2"));

              box.add(Box.createHorizontalGlue());

              box.add(new JButton("按钮3"));

              box.add(Box.createHorizontalStrut(10));

               

              Glue

              Glue插入在两组件之间时,它会将两组件挤到最左与最右(或最上与最下),不可见的glue将会占满整个中间的空间。


              box.add(new JButton("按钮1"));

              box.add(Box.createHorizontalGlue());

              box.add(new JButton("按钮2"));

              box.add(Box.createRigidArea(new Dimension(50, 50)));

              box.add(new JButton("按钮3"));

               

              Filler

              FillerBox的内部类,它的功能跟Rigid类似,都可以指定长宽的大小限制,且Filler可以指定最大、较佳、最小的长宽大小。

              构造函数:

              Box.Filler(Dimension min, Dimension pref, Dimension max)


              评论 1
              添加红包

              请填写红包祝福语或标题

              红包个数最小为10个

              红包金额最低5元

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

              抵扣说明:

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

              余额充值