GUI编程 (狂神学习笔记)2021-10-8

GUI 编程


告诉大家该怎么学?

  • 这是什么?
  • 它怎么玩
  • 该如何去在我们平时运用?
  • class-可阅读的

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听时间
  • 鼠标
  • 键盘事件
  • 外挂
  • 破解工具

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 组件和容器

1.2.1 Frame
package com.tree.lesson01;
import java.awt.*;
/**
 * GUI 的第一个界面
 */
public class TestFrame {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame("我的java程序");

        //设置窗口大小
        frame.setSize(400,400);
        // 设置窗口位置
        frame.setLocation(200,200);
        //设置背景颜色
        frame.setBackground(Color.gray);
        //设置可见性
        frame.setVisible(true);
        //设置窗口拉伸
        frame.setResizable(false);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GhtqF6b2-1633706763750)(GUI编程.assets/2325334-20210317161930105-314930371.png)]

/**
 * 创建多个窗口
 */
public class TestFrame2 {
   
    public static void main(String[] args) {
   
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.red);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.blue);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.green);
    }
}
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);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rAg1aiIO-1633706763754)(GUI编程.assets/2325334-20210317161947465-1768301913.png)]

1.2.2. Panel(面板)
public class TestPanel {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        Panel panel = new Panel();
        //对窗口设置布局、可见性
        frame.setLayout(null);
        frame.setVisible(true);
        //窗口的坐标
        frame.setBounds(100,100,400,400);
        frame.setBackground(Color.red);
        //相对于frame的面板坐标
        panel.setBounds(50,50,300,300);
        panel.setBackground(Color.yellow);
        //将面板添加到窗口
        frame.add(panel);

        frame.addWindowListener(new WindowAdapter() {
   
            @Override
            public void windowClosing(WindowEvent e) {
   
                System.exit(0);
            }
        });
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wq5jgLHK-1633706763764)(GUI编程.assets/2325334-20210317162003068-2097307029.png)]

2.3 布局管理器

  • 流式布局
public class TestFlowLayout {
     //流式布局
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        frame.setVisible(true);
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");

        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(300,200);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XwQRgazw-1633706763769)(GUI编程.assets/2325334-20210317162013759-127782931.png)]

  • 东西南北中
public class TestBorderLayout {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        frame.setVisible(true);
        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(300,200);

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U3A4UHWT-1633706763773)(GUI编程.assets/2325334-20210317162025494-1657290476.png)]

  • 表格布局
public class TestGridLayout {
   
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        //设置表格布局 3*2
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.setVisible(true);
        //将表格自动填充于窗口
        frame.pack();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L8ZoyI4B-1633706763777)(GUI编程.assets/2325334-20210317162037528-294596330.png)]

课堂练习

/**
 * 坐标定位  布局法
 */
public class TestHomeWork {
   
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        Button button7 = new Button("button7");
        Button button8 = new Button("button8");
        Button button9 = new Button("button9");
        Button button10 = new Button("button10");

        button1.setBounds(0,0,100,200);
        frame.add(button1);
        button2.setBounds(100,0,400,100);
        frame.add(button2);
        button3.setBounds(100,100,400,100);
        frame.add(button3);
        button4.setBounds(500,0,100,200);
        frame.add(<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值