GUI编程

GUI编程

1、简介

怎么学?

  • 是什么?
  • 怎么用?
  • 在平时中去应用?

组件

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

2、AWT简介

Gui的核心技术:Swing AWT(abstract window tools)
1.界面不太美观
2.需要jre运行环境
为什么要学习?
1.写出自己想要的一些小工具
2.工作中是,需要维护swing界面
3.了解MVC架构,了解监听

2.1、AWT介绍

1.包含很多类和接口!GUI!
2.元素:窗口,按钮,文本框
3.java.awt

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mvAVgW1w-1644412214517)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220126225056267.png)]

2.2、组件和容器

1、Frame

public class testFrame01 {
    public static void main(String[] args) {

        //Frame,JDK,看源码
        Frame frame = new Frame("我的第一个Java图像界面窗口");

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

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

        //设置背景颜色 Color
        frame.setBackground(new Color(132, 23, 199));

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

        //设置窗口大小固定
        frame.setResizable(false);//窗口大小不可以改变
    }
}

2、封装

public class testFrame2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        MyFrame myFrame1 =new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2 =new MyFrame(100,300,200,200,Color.pink);
        MyFrame myFrame3 =new MyFrame(300,100,200,200,Color.black);
        MyFrame myFrame4 =new MyFrame(300,300,200,200,Color.orange);

    }
}
class MyFrame extends Frame {//子类继承父类的所有特性
    static int id = 0;//可能存在多个窗口,需要一个计数器,默认窗口的id为0

    public MyFrame(int x,int y,int w,int h,Color color){//创建一个构造器,定义几个变量参数
        super("MyFrame+"+(++id));//调用父类构造器,每new一个就让id自增1
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);//封装,继承了父类的方法

    }


}

实现效果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9V4McMKj-1644412214519)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220126231524504.png)]

3、面板Panel

解决关闭事件

//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(6, 85, 173));

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

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

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AiwJobfC-1644412214520)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220127214710638.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(FlowLayout.LEFT));

        frame.setSize(200,200);
        //把按钮添加进去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TyQVg8wm-1644412214521)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220127220425668.png)]

  • 东西南北中

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

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BgyQ4bQJ-1644412214522)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220127221805400.png)]

  • 表格布局

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        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(2,3));//设置布局
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();//Java函数,自动设置布局
        frame.setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hgA03mvo-1644412214522)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220127222757266.png)]

课堂练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2vzbfy4H-1644412214525)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220127223152128.png)]

代码实现

public class practise01 {
    public static void main(String[] args) {

        //总 Frame
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(400,400);
        frame.setBackground(Color.blue);
        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("panel2-btn-1"));
        panel2.add(new Button("panel2-btn-2"));

        //将panel2加入到panel1中
        panel1.add(panel2,BorderLayout.CENTER);//布局

        //下面
        panel3.add(new Button("East-2"),BorderLayout.EAST);
        panel3.add(new Button("West-2"),BorderLayout.WEST);
        //下面中间
        panel4.add(new Button("panel4-btn-1"));
        panel4.add(new Button("panel4-btn-2"));
        panel4.add(new Button("panel4-btn-3"));
        panel4.add(new Button("panel4-btn-4"));

        //for循环进行添加panel4的按钮
//        for (int i = 0; i < 4; i++) {
//            panel4.add(new Button("for-"+i));
//        }
        panel3.add(panel4,BorderLayout.CENTER);

        //将panel面板加入到frame中
       frame.add(panel1);
       frame.add(panel3);

       //增加监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WkR8piBf-1644412214527)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220201210247723.png)]

总结:

1.Frame是一个顶级窗口

2.Panel无法单独显示,必须添加到某个容器中

3.布局管理器

​ 1、流式

​ 2、东西南北中

​ 3、表格

4.大小,定位,背景颜色,可见性,监听!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇_码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值