【狂神说Java】阶段一笔记24. java的GUI编程01-AWT

组件窗口弹窗面板文本框列表框按钮图片监听事件鼠标键盘事件破解工具:反编译 class文件 -> 可阅读1 简介GUI的核心技术:Swing AWT因为界面不美观需要jre环境!为什么要学习:可以写出自己的一些小工具2. 工作时候,可能需要维护swing界面,概率极小3. 了解MVC架构,了解监听!2 AWT2.1 AWT介绍包含了很多类和接口! GUI:图形用户界面编程元素:窗口,按钮,
摘要由CSDN通过智能技术生成

1 简介

GUI的核心技术:Swing AWT

  1. 因为界面不美观

  2. 需要jre环境!

为什么要学习:

  1. 可以写出自己的一些小工具
    2. 工作时候,可能需要维护swing界面,概率极小
    3. 了解MVC架构,了解监听!

2 AWT

2.1 AWT介绍

  1. 包含了很多类和接口! GUI:图形用户界面编程
  2. 元素:窗口,按钮,文本框
  3. java.awt

在这里插入图片描述

2.2 组件和容器

2.2.1 Frame

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame, JDK, 看源码!
        Frame frame = new Frame("我的第一个java图形界面窗口");

        //需要设置可见性 w h
        frame.setVisible(true);

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

        //设置背景颜色 Color
        frame.setBackground(new Color(50, 175, 137));

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

        //设置大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述

问题:发现窗口关闭不掉,停止java程序

2.2.2 回顾封装

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.MAGENTA);
    }
}

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

    //子类构造器
    public MyFrame(int x, int y, int w, int h, Color color){
        //父类构造器:每调用一次自增1
        super("MyFrame" + (++id));

        setVisible(true);
        setBounds(x, y, w, h);
        setBackground(color);
    }
}

在这里插入图片描述

2.2.3 面板Panel

public class TestPanel {
    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(new Color(76, 150, 125));

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

        //frame.add(panel); 面板放入窗口中
        frame.add(panel);

        frame.setVisible(true);

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

解决了关闭事件

在这里插入图片描述

2.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(FlowLayout.LEFT));
        //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(200, 200);

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

        frame.setVisible(true);
    }
}
  • 东西南北中
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        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);
    }
}
  • 表格布局
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);
    }
}

2.3.1 练习

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

        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2, 1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2, 2));

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

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

        frame.add(panel1, BorderLayout.SOUTH);
        frame.add(panel3, BorderLayout.NORTH);
    }
}

在这里插入图片描述

2.3.2 总结

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中
  3. 布局管理器:流式、东西南北中、表格
  4. 大小、定位、背景颜色、可见性、监听!

2.4 事件监听

  1. 当某件事情发生的时候,需要做什么?
public class TestAction {
    public static void main(String[] args) {
        //按下按钮的时候,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();

        //因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        //文本框

        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);
    }

    //关闭窗体的事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//事件监听
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}
  1. 多个按钮共享一个事件
public class TestAction2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始  停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        MyMonitor myMonitor = new MyMonitor();

        //可以显示的定义触发会返回的命令,如果不显示定义则会显示默认的值
        //可以多个按钮可以只写一个监听类
        button2.setActionCommand("button2-stop");
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1, BorderLayout.SOUTH);
        frame.add(button2, BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);
    }
}

class MyMonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取按钮上的信息
        System.out.println("按钮被点击了:msg=> " + e.getActionCommand());
        if (e.getActionCommand().equals("start")){

        }
    }
}

2.5 输入框 TextField 监听

public class TestText01 {
    public static void main(String[] args) {
        //只管启动!
        new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);

        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);

        //设置替换编码
        textField.setEchoChar('*');

        pack();
        setVisible(true);
    }
}

class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得一些资源,返回一个对象
        TextField field = (TextField) e.getSource();
        // 获得输入框中的文本
        System.out.println(field.getText());
        //null
        field.setText("");
    }
}

2.6 简易计算器,组合+内部类回顾复习!

oop原则:组合大于继承!

class A extends B{

}

//组合
class A{
    public B b;
}
//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
    }
}

//计算器类
class Calculator extends Frame{
    public Calculator() {
        //3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));

        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

//监听器类
class MyCalculatorListener implements ActionListener{

    //获取三个变量
    private  TextField num1, num2, num3;

    public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        //2.将这个值加法运算后,放到第三个框
        num3.setText("" + (n1 + n2));

        //3.清楚前两个框
        num1.setText("");
        num2.setText("");
    }
}

完全改变为面向对象

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame{
    //3个文本框
    TextField num1, num2, num3;

    //方法
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(this));

        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

    public Calculator() {
    }
}

//监听器类
class MyCalculatorListener implements ActionListener{

    //获取计算器这个对象,在一个类中组合另外一个类
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator){
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());

        //2.将这个值加法运算后,放到第三个框
        calculator.num3.setText("" + (n1 + n2));

        //3.清楚前两个框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

内部类:更好的包装

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame{
    //3个文本框
    TextField num1, num2, num3;

    //方法
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener());

        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

    //监听器
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法
    private class MyCalculatorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            //2.将这个值加法运算后,放到第三个框
            num3.setText("" + (n1 + n2));

            //3.清楚前两个框
            num1.setText("");
            num2.setText("");
        }
    }

    public Calculator() {
    }
}

2.7 画笔

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{

    public void loadFrame(){
        setBounds(200, 200, 600, 500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画笔,需要有颜色,画笔可以画画
        g.setColor(Color.red);
        //g.drawOval(100, 100, 200, 200);
        g.fillOval(100, 100, 100, 100);

        g.setColor(Color.GREEN);
        g.fillRect(150, 200, 200, 200);

        //养成习惯,画笔用完将他还原到最初的颜色
    }
}

2.8 鼠标监听

目的:想要实现鼠标画画!

//鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

//自己的类
class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;

    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 400, 300);

        //存鼠标点击的点
        points = new ArrayList<>();

        //鼠标监听器相对于窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }

    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }

    //适配器模式减少实现的方法
    private class MyMouseListener extends MouseAdapter {
        //鼠标 按下,弹起,按住不放

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            //点击的时候,就会在界面上产生一个点!画
            //这个点就是鼠标的点:
            frame.addPaint(new Point(e.getX(), e.getY()));

            //每次点击鼠标都需要重新画一遍,刷新
            frame.repaint();
        }
    }
}

在这里插入图片描述

2.9 窗口监听

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame{
    public WindowFrame() {
        setBackground(Color.BLUE);
        setBounds(100, 100, 200, 200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());

        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                        System.exit(0);
                    }

                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("被激活了");
                        System.out.println("windowActivated");
                    }
                }
        );
    }

    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false); //隐藏窗口,通过按钮隐藏当前窗口
            System.exit(0); //正常退出
        }
    }
}

2.10 键盘监听

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(1, 2, 300, 400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前键盘的码
                int keyCode = e.getKeyCode(); //不需要去记录这个数值,直接使用静态属性VK_XXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP){
                   System.out.println("你按下了上键");
                }
                //根据按下的不同操作,产生不同的结果
            }
        });
    }
}

PS:学习自狂神说java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值