三种布局管理器

三种布局管理器

1.流式布局:

package com.zhang.Study.三种布局管理.流式布局;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 流式布局:线性布局
 */
public class TestFlowLayout {
    public static void main(String[] args) {
        //创建一个新的窗口
        Frame frame = new Frame();
        frame.setBounds(400,400,400,400);//设置初始位置和窗口大小
        frame.setBackground(new Color(234, 185, 185));
        frame.setVisible(true);//显示窗口
        //设置流式布局
        frame.setLayout(new FlowLayout());//流式布局,默认居中排列
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//设置向左排列
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//设置向右排列


        //创建按钮
        Button button = new Button("button1");
        Button button2 = new Button("button二");
        Button button3= new Button("button3");
        Button button4 = new Button("button4");
        //在界面上添加按钮
        frame.add(button);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        //监听事件关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

东西南北布局:

package com.zhang.Study.三种布局管理.东西南北中布局;
/**
 * 东西南北布局
 *
 */

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Application {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(400,400,400,400);
        frame.setVisible(true);
        Button east = new Button("east");
        Button west = new Button("west");
        Button north = new Button("north");
        Button south = new Button("south");
        Button center = new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(center,BorderLayout.CENTER);


        //监听事件关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });


    }
}

栅格布局:

package com.zhang.Study.三种布局管理.栅格布局;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 栅格布局GridLayout(m,n)可以把布局设置成m行n列的表格
 */
public class TestGridUse {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(400,400,400,400);

        Button button1 = new Button("btn1");
        Button button2 = new Button("btn2");
        Button button3 = new Button("btn3");
        Button button4 = new Button("btn4");
        Button button5 = new Button("btn5");
        Button button6 = new Button("btn6");
        //设置栅格布局,栅格
        frame.setLayout(new  GridLayout(2,3));
        //把按钮添加到窗口
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.setVisible(true);
        //监听事件关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

 

posted on 2021-11-26 16:24  张铁蛋666  阅读(67)  评论(0编辑  收藏  举报

导航