zoukankan      html  css  js  c++  java
  • GUI编程(四)-----布局管理器

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

    常用的布局管理器有: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

    流式布局管理器(FlowLayout)

    摆放风格:从左到右从上到下依次摆放。

    流式布局管理器要注意的事项:
        1. 流式布局管理器默认情况是居中对齐的。
        2. panel默认的局部管理器就是FlowLayout。

    代码示例如下:

     1 public static void main(String[] args) {
     2         JFrame frame = new JFrame("窗体");
     3         //创建面板
     4         JPanel panel = new JPanel();
     5         frame.add(panel);
     6         //创建一个流式布局管理器
     7         FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 10);// FlowLayout.LEFT 指定对齐的方式。
     8         //让流式布局管理器管理frame窗体
     9         panel.setLayout(flowLayout);
    10         
    11         panel.add(new JButton("按钮1"));
    12         panel.add(new JButton("按钮2"));
    13         panel.add(new JButton("按钮3"));
    14         panel.add(new JButton("按钮4"));
    15         
    16         //初始化窗体
    17         frame.setSize(300, 300);
    18         frame.setLocationRelativeTo(null);
    19         frame.setVisible(true);
    20     }
    View Code

    表格布局管理器(GridLayout)

    摆放风格:类似于网格。

    注意的事项: 如果表格数量不够使用时,默认会多加一列。

    代码示例如下:

     1 public static void main(String[] args) {
     2         JFrame frame = new JFrame("计算器");
     3         //创建表格布局管理器
     4         GridLayout gridLayout = new GridLayout(4, 4, 1, 2);
     5         
     6         //让窗体交给表格布局管理器管理
     7         frame.setLayout(gridLayout);
     8         for(int i = 0 ; i<10; i++){
     9             frame.add(new JButton(i+""));
    10         }
    11         frame.add(new JButton("+"));
    12         frame.add(new JButton("-"));
    13         frame.add(new JButton("*"));
    14         frame.add(new JButton("/"));
    15         frame.add(new JButton("="));
    16         frame.add(new JButton("."));
    17         
    18 //        frame.add(new JButton("aa"));
    19         
    20         //初始化窗体
    21         frame.setSize(300, 300);
    22         frame.setLocationRelativeTo(null);
    23         frame.setVisible(true);
    24         
    25     }
    View Code

    卡片布局管理器(CardLayout)

    摆放风格:卡片式摆放。

    注意事项:需要借助于时间才能进行卡片的切换。

    代码示例:

     1 public static void main(String[] args) {
     2         JFrame frame = new JFrame("卡片布局管理器");
     3         final JPanel panel = new JPanel();
     4         frame.add(panel);
     5         
     6         //创建一个卡片布局管理器
     7         final CardLayout cardLayout = new CardLayout();
     8         panel.setLayout(cardLayout);
     9         
    10         //往面板添加数据
    11         JButton button = new JButton("黑桃A");
    12         panel.add(button);
    13         panel.add(new JButton("红桃K"));
    14         panel.add(new JButton("梅花6"));
    15         panel.add(new JButton("方块2"));
    16         
    17         button.addActionListener(new ActionListener() {
    18             
    19             @Override
    20             public void actionPerformed(ActionEvent e) {
    21                 cardLayout.next(panel);  //下一张
    22 //                cardLayout.previous(parent);  上一张
    23             }
    24         });
    25         //初始化窗体
    26         frame.setSize(300, 300);
    27         frame.setLocationRelativeTo(null);
    28         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    29         frame.setVisible(true);
    30     }
    View Code
  • 相关阅读:
    最长公共子序列和最长公共连续子序列 andrew的日志 网易博客
    Best Computer Science Interview Books JiansNet
    分享:Mitmproxy 0.9 发布,支持 SSL 的 HTTP 代理
    创业环境差,被迫“走西口”:一位台湾码农的心路历程
    STLSupport GDB Wiki
    utils C/C++的常用工具类
    题目36 ACM在线评测系统
    DRAMA QUEEN_洪晃在ilook的BLOG
    Codeblocks调试STL——gdb with python support Wanglikai91 博客园
    python urlencode 编码
  • 原文地址:https://www.cnblogs.com/nicker/p/6268913.html
Copyright © 2011-2022 走看看