布局管理器:布局管理就是用于指定组件的 摆放位置的。

常用的布局管理器有:BorderLayout(边框布局管理器)、流式布局管理器(FlowLayout)、 表格布局管理器(GridLayout)、卡片布局管理器(CardLayout)。

每种布局管理器都有自己的摆放风格,下面具体看一下各自风格和用法:

BorderLayout(边框布局管理器)

摆放的风格: 上北  、 下南 、 左西、 右东 , 中。

Borderlayout 要注意的事项:
    1. 使用Borderlayout添加组件的时候,如果没有指定组件的方位,那么默认添加到中间的位置上。
    2. 使用BorderLayout的时候,如果东南西北那个方向没有对应的组件,那么中间位置的组件就会占据其空缺的位置。
    3. 窗体默认的布局管理器就是Borderlayout。

代码示例如下:

 1 public static void main(String[] args) {
 2         JFrame frame = new JFrame("边框局部管理器");
 3         //创建一个边框布局管理器
 4         BorderLayout borderLayout = new BorderLayout();
 5         //让borderlayout管理frame窗体。
 6         frame.setLayout(borderLayout);
 7         
 8         frame.add(new JButton("北"),BorderLayout.NORTH);
 9         frame.add(new JButton("南"),BorderLayout.SOUTH);
10         frame.add(new JButton("西"),BorderLayout.WEST);
11         frame.add(new JButton("东"),BorderLayout.EAST);
12         frame.add(new JButton("中"),BorderLayout.CENTER);
13         //初始化窗体
14         frame.setSize(300, 300);
15         frame.setLocationRelativeTo(null);
16         frame.setVisible(true);
17     }
View Code

相关文章:

  • 2022-12-23
  • 2021-04-12
  • 2021-09-19
  • 2021-12-24
  • 2021-05-24
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-16
  • 2021-08-22
  • 2022-02-28
  • 2021-10-20
  • 2022-01-24
相关资源
相似解决方案