5、布局管理器

5、布局管理器

一、流式布局

package com.dryant.lesson1;

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();


        Button button1 = new Button("bt1");
        Button button2 = new Button("bt2");
        Button button3 = new Button("bt3");
        Button button4 = new Button("bt4");
        Button button5 = new Button("bt5");

        //frame.setLayout(new FlowLayout());设置流式布局
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//设置流式布局左对齐
        frame.setSize(200,200);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);

        frame.setVisible(true);
    }
}

二、东西南北中布局

package com.dryant.lesson1;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        ///frame.setLayout(new BorderLayout());
        frame.setSize(500,500);
        Button button1 = new Button("east");
        Button button2 = new Button("按钮西");
        Button button3 = new Button("按钮南");
        Button button4 = new Button("按钮北");
        Button button5 = new Button("按钮中");
        frame.setVisible(true);
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);

    }
}

三、表格布局

package com.dryant.lesson1;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.setVisible(true);
        frame.setSize(300,300);

    }
}

你可能感兴趣的:(Java_GUI,java)