GUI programming study notes 2-AWT related (layout)

GUI programming study notes 2-AWT related (layout)


2.1 Introduction to AWT (Abstract Window Tool)

  • Contains many classes and interfaces
  • Component
    • Basic components (can be used directly, to be added to the container), button, TextArea, Lable, etc.
    • Container
      • Windows
        • Frame window (top window)
        • Popup Dialog
      • Panel (can not be displayed separately, need to be added to a container)
        • Applet for web pages is no longer used

Insert picture description here


2.2 Components and containers

1、Frame

My first Java GUI window

package pers.ylw.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {

        //Frame对象,这时候new的窗口是在内存里的看不见
        Frame frame = new Frame("我的第一个Java图形界面窗口");

        //设置Frame窗口可见性
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);

        //设置弹出的初始位置
        frame.setLocation(200,200);

		//setBounds(x, y, w, h);和上面两行代码效果一样

        //设置窗口背景颜色,需要Color类
        frame.setBackground(new Color(248, 21, 87));

        //设置窗口大小固定(不能使用鼠标拖拽边界使窗口尺寸改变)
        frame.setResizable(false);

    }
}

Effect It is
Insert picture description here
not possible to close the window by the X in the upper right corner, you can close the window by terminating the program

Insert picture description here

Define your own class and create multiple windows

package pers.ylw.lesson01;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.black);
    }
}

class MyFrame extends Frame{
    static int id = 0; //可能存在多个窗口,需要一个计数器

    //封装TextFrame类,x和y是窗口初始位置,w和h是宽高
    public MyFrame(int x,int y,int w,int h,Color color){
        super("我的Frame"+(++id));
        //设置窗口在屏幕的位置和窗口的宽高
        setBounds(x, y, w, h);
        //设置Frame窗口可见性
        setVisible(true);
        //设置颜色
        setBackground(color);
    }
}

effect
Insert picture description here

2、Panel

panel panel and click close event

package pers.ylw.lesson01;

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

//panel可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //panel 有布局的概念
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);

        //坐标和颜色
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(213, 11, 11));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(137, 77, 220));

        //在frame里添加panel
        frame.add(panel);//查看panel源码可以看到它继承了Component所以可以添加

        //设置可见性
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件
        //如果new WindowListener,需要重写很多方法
        //所以使用适配器模式new WidowAdapter,只需要重写自己想使用的方法就可以了
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //窗口点击关闭的时候需要做的事情
                System.exit(0);
            }
        });

    }
}

effect
Insert picture description here

3. Layout Manager

Flow layout FlowLayout, horizontal swing control

package pers.ylw.lesson01;

import java.awt.*;

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

        //组件-按钮,参数是按钮显示的文字
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置为流式布局, 括号里的参数可以填靠左、靠右等,不写默认居中
        //frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.setSize(200,200);

        //把按钮放上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        //设置可见
        frame.setVisible(true);
    }
}

effect
Insert picture description here

BorderLayout

package pers.ylw.lesson01;

import java.awt.*;

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

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

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

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

    }
}

effect
Insert picture description here

Table layout GridLayout

package pers.ylw.lesson01;

import java.awt.*;

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

        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");

        //3行2列,后面还有两个参数是行间距和列间距,不设置默认为0
        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.pack();//自动选择优秀的布局,不需要自己设置大小
        frame.setVisible(true);

    }
}

effect

Insert picture description here


Exercise

Implement the layout
Insert picture description here

package pers.ylw.lesson01;

import java.awt.*;

public class ExDemo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setSize(400,200);
        frame.setLocation(300,400);
        //frame.setBackground(new Color(1,1,1));
        //两行一列
        frame.setLayout(new GridLayout(2,1));

        //4个面板
        Panel pl = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        //上半部分的左右两个按钮
        pl.add(new Button("East-1"),BorderLayout.EAST);
        pl.add(new Button("West-1"),BorderLayout.WEST);
        //上半部分的中间两个按钮
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        //p2装到p1的中间部分
        pl.add(p2,BorderLayout.CENTER);

        //下半部分的左右两个按钮
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);
        //下半部分的中间4个按钮
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("p4-bnt-" + i));
        }
        //p4装到p3的中间部分
        p3.add(p4,BorderLayout.CENTER);

        //把面板放到frame里
        frame.add(pl);
        frame.add(p3);

    }
}

effect
Insert picture description here

Published 318 original articles · Like 44 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/qq_43594119/article/details/105669406