GUI入门(AWT)

GUI编程

GUI怎么学?

  1. GUI是什么?

    Graphical User Interface 图形用户接口

  2. GUI怎么玩?

  3. 平时如何运用?

组件:

  1. 窗口
  2. 弹窗
  3. 面板
  4. 文本框
  5. 列表框
  6. 按钮
  7. 图片
  8. 监听事件
  9. 鼠标事件
  10. 键盘事件
  11. 外挂
  12. 破解工具

1. 简介

GUI的核心技术:Swing AWT
  1. 因为界面不美观

  2. 需要JRE环境

    (所以不流行)

为什么要学习GUI?
  1. 可以写出自己心中想要的小工具
  2. 工作中可能需要维护到Swing界面 (概率极小)
  3. 了解MVC架构,了解监听器

2. AWT(Abstract Window Toolkit)

2.1 AWT介绍

​ - 包含很多类和接口 实现GUI编程

​ - 包含很多元素:窗口、按钮、文本框

- java.awt包中两个常用的类:Frame
2.2 组件跟容器
  • Frame(窗口):

    package com.ma.guistudy;
    
    import java.awt.*;
    
    //GUI的第一个界面
    public class TestFrame {
        public static void main(String[] args) {
            //Java中有Frame类,可以自行看源码进行学习
            Frame frame = new Frame("我的第一个Java图形界面窗口");
            
            //设置可见性
            frame.setVisible(true);
    
            //设置窗口大小
            frame.setSize(400,400);
            //颜色 Color
            frame.setBackground(Color.CYAN);
            //弹出的初始位置
            frame.setLocation(200,200);
            //设置大小固定,不可变大小
            frame.setResizable(false);
          
        }
    }
    
    

在这里插入图片描述

  • 工具类:

    package com.ma.guistudy;
    
    import java.awt.*;
    
    // 创建一个类继承Frame类,拥有Frame的所有特性,然后可以添加一些自己想要的属性
    class MyFrame extends Frame {
        //因为不知一个窗口,所以加个编号,便于计数
        static int id = 0;
        //构造器,添加初始化的属性
        public MyFrame(int x,
                       int y,
                       int w,
                       int h,
                       Color color,
                       boolean a,
                       boolean b){
            //调用Frame,即该类的父类
            super("MyFrame"+(++id));
            setLocation(x,y);
            setSize(w,h);
            setBackground(color);
            setVisible(a);
            setResizable(b);
        }
    }
    
    public class TestFrame2 {
        public static void main(String[] args) {
            MyFrame myFrame = new MyFrame(400, 400, 100, 100, Color.red, true, false);
        }
    
    }
    
  • Pannel(面板):

    • 面板不能单独存在,必须在Frame窗口中
    package com.ma.guistudy;
    
    import java.awt.*;
    
    public class PanelStudy {
        public static void main(String[] args) {
            Frame frame = new Frame();
            //查看源代码学习(Pannel必须在Frame中)
            Panel panel = new Panel();
    
            //窗口布局
            frame.setLayout(null);
            //名字
            frame.setTitle("窗口");
            //坐标
            frame.setBounds(300,300,500,500);
            //颜色
            Color color = Color.black;
            frame.setBackground(color);
            //显示
            frame.setVisible(true);
            //Panel是在窗口中的
            //坐标
            panel.setBounds(100,100,100,100);
            //颜色
            Color color1 = Color.white;
            panel.setBackground(color1);
    
            //把面板添加到窗口中
        
            frame.add(panel);
        }
    }
    
//添加监听事件,窗口关闭事件
//适配器模式:选择合适的相配
WindowListener windowListener = new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        //窗口关闭的时候需要做的事情
        System.exit(0);
    }
};
//监听事件--结束程序
frame.addWindowListener(windowListener);
2.3布局管理器
  • 流式布局:

    package com.ma.guistudy;
    
    import java.awt.*;
    
    public class TestFlowLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
            Panel panel = new Panel();
            //按钮组件
            Button button1 = new Button("button1");
            Button button2 = new Button("button2");
            Button button3 = new Button("button3");
            frame.setSize(500,500);
            frame.setVisible(true);
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            //Frame设置流式布局
            frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        }
    }
    
  • 东西南北中

    package com.ma.guistudy;
    
    import java.awt.*;
    
    public class TestFlowLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
            Panel panel = new Panel();
            //按钮组件
            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.setSize(500,500);
            frame.setVisible(true);
            //东西南北中布局
            frame.add(center,BorderLayout.CENTER);
            frame.add(east,BorderLayout.EAST);
            frame.add(west,BorderLayout.WEST);
            frame.add(south,BorderLayout.SOUTH);
            frame.add(north,BorderLayout.NORTH);
    
        }
    }
    

在这里插入图片描述

  • 表格布局

    package com.ma.guistudy;
    
    import java.awt.*;
    
    public class TestFlowLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
            Panel panel = new Panel();
            //按钮组件
            Button button1 = new Button("button1");
            Button button2 = new Button("button2");
            Button button3 = new Button("button3");
            Button button4 = new Button("button4");
            Button button5 = new Button("button5");
            frame.setSize(500,500);
            frame.setVisible(true);
            //东西南北中布局
            frame.setLayout(new GridLayout(1,0));
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.add(button4);
            frame.add(button5);
            //自动布局
            frame.pack();
        }
    }
    

在这里插入图片描述

2.4 事件监听
  • 事件监听:当某个事情发生的时候,做什么(例:按下按钮触发事件)
package com.ma.guistudy;

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

//事件监听
public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮触发事件
        Frame frame = new Frame();
        Button button = new Button();
        // 添加事件监听 这里()中需要一个new ActionListener
        //原理:点击按钮调用ActionListener的实现类运行实现类中的功能
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button);
        frame.setVisible(true);
        //关闭窗口
        colseWindow(frame);
    }
    //窗口关闭事件
    public static void colseWindow(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("喵喵喵");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值