package com.he.swing;
import javax.swing.*;
/
- @description: JavaSwing_4_BoxLayout 箱式布局
- @author: ShouSi
- @createDate: 2021/11/1 */ public class JavaSwing_4_BoxLayout {
public static void main(String[] args) {
JFrame jf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JFrame("测试窗口"<span style="color: rgba(0, 0, 0, 1)">); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JButton btn01 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JButton("Button01"<span style="color: rgba(0, 0, 0, 1)">); JButton btn02 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JButton("Button02"<span style="color: rgba(0, 0, 0, 1)">); JButton btn03 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JButton("Button03"<span style="color: rgba(0, 0, 0, 1)">); JButton btn04 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JButton("Button04"<span style="color: rgba(0, 0, 0, 1)">); JButton btn05 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JButton("Button05"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建第一个水平箱容器</span> Box hBox01 =<span style="color: rgba(0, 0, 0, 1)"> Box.createHorizontalBox(); hBox01.add(btn01); hBox01.add(btn02); hBox01.add(btn03); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建第二水平箱容器</span> Box hBox02 =<span style="color: rgba(0, 0, 0, 1)"> Box.createHorizontalBox(); hBox02.add(btn04); hBox02.add(Box.createHorizontalGlue()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加一个水平方向胶状的不可见组件,撑满剩余水平空间</span>hBox02.add(btn05);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个垂直箱容器,放置上面两个水平箱(Box组合嵌套)</span> Box vBox =<span style="color: rgba(0, 0, 0, 1)"> Box.createVerticalBox(); vBox.add(hBox01); vBox.add(hBox02); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 把垂直箱容器作为内容面板设置到窗口</span>jf.setContentPane(vBox);
jf.pack(); jf.setLocationRelativeTo(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); jf.setVisible(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);} }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/6550.html