常用布局管理器

  1.  1 import java.awt.*;
     2 
     3 import javax.swing.*;
     4 
     5 public class AbsolutePosition extends JFrame {
     6     /**
     7      * 
     8      */
     9     private static final long serialVersionUID = 1L;
    10 
    11     public AbsolutePosition() {
    12         setTitle("本窗体使用绝对布局"); // 设置该窗体的标题
    13         setLayout(null); // 使该窗体取消布局管理器设置
    14         setBounds(0, 0, 200, 150); // 绝对定位窗体的位置与大小
    15         Container c = getContentPane(); // 创建容器对象
    16         JButton b1 = new JButton("按钮1"); // 创建按钮
    17         JButton b2 = new JButton("按钮2"); // 创建按钮
    18         b1.setBounds(10, 30, 80, 30); // 设置按钮的位置与大小
    19         b2.setBounds(60, 70, 100, 20);
    20         c.add(b1); // 将按钮添加到容器中
    21         c.add(b2);
    22         setVisible(true); // 使窗体可见
    23         // 设置窗体关闭方式
    24         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    25     }
    26     
    27     public static void main(String[] args) {
    28         new AbsolutePosition();
    29     }
    30 }

     

    绝对布局

  2. import java.awt.*;
    
    import javax.swing.*;
    
    public class FlowLayoutPosition extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public FlowLayoutPosition() {
            setTitle("本窗体使用流布局管理器"); // 设置窗体标题
            Container c = getContentPane();
            // 设置窗体使用流布局管理器,使组件右对齐,并且设置组件之间的水平间隔与垂直间隔
            setLayout(new FlowLayout(2, 10, 10));
            for (int i = 0; i < 10; i++) { // 在容器中循环添加10个按钮
                c.add(new JButton("button" + i));
            }
            setSize(300, 200); // 设置窗体大小
            setVisible(true); // 设置窗体可见
            // 设置窗体关闭方式
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        }
        
        public static void main(String[] args) {
            new FlowLayoutPosition();
        }
    }

    流布局管理器

  3. import java.awt.*;
    
    import javax.swing.*;
    
    public class BorderLayoutPosition extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        // 定义组件摆放位置的数组
        String[] border = { BorderLayout.CENTER, BorderLayout.NORTH,
                BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST };
        String[] buttonName = { "center button", "north button",
                "south button", "west button", "east button" };
        
        public BorderLayoutPosition() {
            setTitle("这个窗体使用边界布局管理器");
            Container c = getContentPane(); // 定义一个容器
            setLayout(new BorderLayout()); // 设置容器为边界布局管理器
            for (int i = 0; i < border.length; i++) {
                // 在容器中添加按钮,并设置按钮布局
                c.add(border[i], new JButton(buttonName[i]));
            }
            setSize(350, 200); // 设置窗体大小
            setVisible(true); // 使窗体可视
            // 设置窗体关闭方式
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        }
        
        public static void main(String[] args) {
            new BorderLayoutPosition();
        }
    }

     

    边界布局管理器
  4. package com.lzw;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class GridLayoutPosition extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public GridLayoutPosition() {
            Container c = getContentPane();
            // 设置容器使用网格布局管理器,设置7行3列的网格
            setLayout(new GridLayout(7, 3, 5, 5));
            for (int i = 0; i < 20; i++) {
                c.add(new JButton("button" + i)); // 循环添加按钮
            }
            setSize(300, 300);
            setTitle("这是一个使用网格布局管理器的窗体");
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        
        public static void main(String[] args) {
            new GridLayoutPosition();
        }
    }

     

    网络布局管理器
posted @ 2017-10-28 17:08  追逐你的世界  阅读(412)  评论(0编辑  收藏  举报