图形用户界面GUI编程:AWT、frame、panel、布局管理器

148 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 4 天,点击查看活动详情  GUI编程学习笔记,根据目录食用~

1、简介

Gui的核心技术:Swing 、 AWT

1.页面不美观
2.需要 jre环境 ---------->不流行的原因

为什么要学习?

1.可以写出自己心中的一些小工具
2.工作可能会遇到维护Swing界面
3.了解MVC架构,了解监听

2、AWT(Abstract Windows tools)

007eb49b60fff5fc62bff5ef85e9d98.jpg

3、组件和容器

1、frame

fcfd49943da2e67b8c979d3b1254719.png 问题:无法关闭窗口-----只有停止java程序才能关闭

多个窗口

import java.awt.*;

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

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

    public MyFrame(int x, int y, int w, int h,Color color) {
        super("MyFrame+" + (++id));
        setBackground(color);
        setBounds(x, y, w, h);
        setVisible(true);
    }
}

35cf5f6370b2b099f0da75f1d9ecfbb.png

2、面板panel

panle无法单独显示,必须添加到某个容器中。

含关闭窗口操作

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

// 布局
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();

        // 设置布局
        frame.setLayout(null);
        // frame坐标
        frame.setBounds(300,300,400,400);
        frame.setBackground(new Color(200, 160, 90));
        // panel坐标,相对于frame
        panel.setBounds(50,50,300,300);
        panel.setBackground(new Color(190,20,90));

        frame.add(panel);

        frame.setVisible(true);

        // 监听事件 System.exit(0)
        // 适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            // 窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e){
                // 结束程序
                System.exit(0);
            }
        });
    }
}

174ecfd6bb3735dfa9f5848c03d093a.png

3、布局管理器

流式布局

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.CENTER));
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));    // 靠左
//        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));    // 靠右
        
        frame.setSize(400,300);

        // 添加按钮
        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);
            }
        });
    }
}

1b8c2ae7f349393147ad5a8cf601194.png

东西南北中

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.setSize(200, 200);
        frame.setVisible(true);
    }
}

36a875e99752f93e490b70b366709d4.png

表格布局

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

        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();  // java函数,自动布局
        frame.setVisible(true);
    }
}

be6f7ec46a2d24266aa0875a06b68b6.png

练习

public class ExerciseDemo {
    public static void main(String[] args) {
        // 总frame
        Frame frame = new Frame();
        frame.setSize(400, 300);
        frame.setLocation(300, 400);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2, 1));

        // 4个面板
        Panel p1 = 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));

        p1.add(new Button("East-1"), BorderLayout.EAST);
        p1.add(new Button("West-1"), BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2, BorderLayout.CENTER);

        p3.add(new Button("East-2"), BorderLayout.EAST);
        p3.add(new Button("West-2"), BorderLayout.WEST);
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i));
        }
        p3.add(p4, BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);

    }
}

50c004904ca76162de76c96579b34f2.png