AWT Introduction to GUI Programming Core Technology

GUI programming learning day one

Component

  • window
  • Pop-ups
  • panel
  • Text box
  • List box
  • Button
  • image
  • Listen for events
  • mouse
  • Keyboard events
  • Plug-in
  • Cracking tool

Introduction

GUI core technology: Swing, AWT (not popular)

  1. The interface is not beautiful.
  2. Need jre environment.

The main purpose of learning GUI is to understand MVC architecture and understand monitoring.

AWT

AWT introduction

  1. It is a new class that contains many classes and interfaces for GUI programming and graphical user interface programming.
  2. Elements: windows, buttons, text boxes,
  3. java.awt
    Insert picture description here

Components and containers

Frame

public class TestFrame {
    
    
    public static void main(String[] args) {
    
    
        Frame frame = new Frame("我的第一个java图形界面");

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

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

        //设置背景颜色
        frame.setBackground(new Color(206, 33, 47));

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

        //设定窗口大小固定
        frame.setResizable(false);

    }
}

Insert picture description here

Problem, the window cannot be closed!

pannel

Can be regarded as a space, but cannot exist alone

Adding window monitoring can solve the problem that the window cannot be closed

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

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

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

        //坐标
        frame.setBounds(300, 300, 500, 500);
        //背景颜色
        frame.setBackground(Color.blue);

        //panel是设置坐标,相对于Frame
        panel.setBounds(40, 40, 300, 300);
        //设置背景颜色
        panel.setBackground(Color.BLACK);

        //将panel添加到frame中
        frame.add(panel);

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

        //监听事件,监听窗口关闭时间,System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
    
    
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        });
    }
}

Insert picture description here

Layout manager

  • Flow layout

    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("java窗口");
    
            //设置窗口布局为流式布局
            frame.setLayout(new FlowLayout());
            
            //设置窗口背景颜色
            frame.setBackground(Color.blue);
    
            //设置窗口大小和位置
            frame.setBounds(300, 300, 500, 500);
    
            //new多个button
            Button button1 = new Button("button1");
            Button button2 = new Button("button2");
            Button button3 = new Button("button3");
    
            //将button添加到窗口中去
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
    
            //设置可见性
            frame.setVisible(true);
    
            //窗口监听,是的窗口可以退出
            frame.addWindowListener(new WindowAdapter() {
          
          
                @Override
                public void windowClosing(WindowEvent e) {
          
          
                    System.exit(0);
                }
            });
        }
    }
    

Insert picture description here

  • East, West, South, North and Middle

    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    
    public class TestBorderLayout {
          
          
        public static void main(String[] args) {
          
          
            Frame frame = new Frame("java窗口");
            
            //设定为东西南北中布局
            frame.setLayout(new BorderLayout(10, 10));
    
            //设置窗口大小和位置
            frame.setBounds(200, 200, 500, 500);
    
            //设置背景颜色
            frame.setBackground(Color.blue);
    
            //new button
            Button button1 = new Button("east");
            Button button2 = new Button("west");
            Button button3 = new Button("south");
            Button button4 = new Button("north");
            Button button5 = new Button("center");
    
            //添加button到窗口中
            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);
    
            //设定可见性
            frame.setVisible(true);
    
            //监听窗口,使得窗口可以关闭
            frame.addWindowListener(new WindowAdapter() {
          
          
                @Override
                public void windowClosing(WindowEvent e) {
          
          
                    System.exit(0);
                }
            });
        }
    }
    

Insert picture description here

  • Table layout

    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class TestGridLayout {
          
          
        public static void main(String[] args) {
          
          
            Frame frame = new Frame("Grid");
    
            //设定窗口布局为grid布局
            frame.setLayout(new GridLayout(2, 2, 10, 10));
    
            //设置窗口大小和位置
            frame.setBounds(200, 200, 500, 500);
    
            //设置窗口背景颜色
            frame.setBackground(Color.blue);
    
            //new button
            Button button1 = new Button("button1");
            Button button2 = new Button("button2");
            Button button3 = new Button("button3");
            Button button4 = new Button("button4");
    
            //将button添加到frame窗口中
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.add(button4);
            
            //自动对齐
            frame.pack();
            //设置可见性
            frame.setVisible(true);
    
            //监听窗口,使得其可以被关闭
            frame.addWindowListener(new WindowAdapter() {
          
          
                @Override
                public void windowClosing(WindowEvent e) {
          
          
                    System.exit(0);
                }
            });
        }
    }
    
    

    Insert picture description here

Exercise

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

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

        //设置窗口布局
        frame.setLayout(new BorderLayout());

        //设置窗口大小和位置
        frame.setBounds(200, 200, 500, 500);

        //new panel
        Panel panel1 = new Panel(new GridLayout(2, 1));
        Panel panel2 = new Panel(new GridLayout(4, 1));
        Panel panel3 = new Panel(new GridLayout(2, 1));

        //new button
        Button button1 = new Button("pa1-1");
        Button button2 = new Button("pa1-2");
        Button button3 = new Button("pa2-1");
        Button button4 = new Button("pa2-2");
        Button button5 = new Button("pa2-3");
        Button button6 = new Button("pa2-4");
        Button button7 = new Button("pa3-1");
        Button button8 = new Button("pa3-2");


        //panel1中添加button
        panel1.add(button1);
        panel1.add(button2);

        //panel2中添加button
        panel2.add(button3);
        panel2.add(button4);
        panel2.add(button5);
        panel2.add(button6);

        //panel3中添加button
        panel3.add(button7);
        panel3.add(button8);

        //窗口中添加panel
        frame.add(panel1, BorderLayout.WEST);
        frame.add(panel2, BorderLayout.CENTER);
        frame.add(panel3, BorderLayout.EAST);

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

        //设置监听窗口,使得窗口可以关闭
        frame.addWindowListener(new WindowAdapter() {
    
    
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                System.exit(0);
            }
        });


    }
}

Insert picture description here

to sum up

  1. Frame is a top-level window
  2. Pannel cannot be displayed separately, it must be added to a container
  3. Window background color, size, position, visibility, monitoring
  4. Layout manager
    • Streaming
    • all directions
    • form

reference

Learning link : https://www.bilibili.com/video/BV1DJ411B75F?p=7

Guess you like

Origin blog.csdn.net/wqs12345612/article/details/111561487