初识GUI编程

  • 常用软件的一些组件:

    • 窗口

    • 弹窗

    • 面板

    • 文本框

    • 列表框

    • 按钮

    • 图片

    • 监听事件

    • 鼠标事件

    • 键盘事件

    • 破解工具


一、简介

  • GUI:图形用户界面编程

  • GUI的核心技术: Swing AWT

  • 不流行的原因:

  1. 因为界面不美观所以不流传

  2. 需要jre环境

  • 我们为什么要学习:

  1. 可以写出自己心中想要的一些小工具

  2. 工作的时候,也可能需要维护到Swing界面,概率极小

  3. 了解MVC架构,了解监听


二、AWT

1.AWT介绍

AWT:抽象的窗口工具包 GUI:图形用户界面编程

  • 介绍:包含了很多类和接口!

  • 元素:窗口、按钮、文本框

  • 包路径:java.awt

初识GUI编程_第1张图片


2.弹窗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(225, 146, 207));
​
        //弹出的初始位置
        frame.setLocation(200,200);
​
        //设置大小固定
        frame.setResizable(false);
    }
}

问题:发现窗口关闭不掉,只需要停止java程序即可

  • 尝试封装:

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        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){
        super("Myframe+"+(++id));
        setBackground(color);
        setVisible(true);
        setBounds(x,y,w,h);
    }
}

3.面板Panel

解决了无法关闭窗口的问题,其实Panel相当于Frame中的一个组件,如果Frame中需要Panel,直接add就可以了

//Panel可以看成是一个空间,但是不能单独存在
public class TestPanel1 {
    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(211, 138, 231));
​
        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(253, 99, 99));
​
        //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);
            }
        });
    }
}

4.布局管理器

  • 流式布局

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.LEFT));
​
        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("TestBorderLayOut");
​
        Button east = new Button("East");
        Button west = new Button("West");
        Button north = new Button("North");
        Button south = new Button("South");
        Button center = new Button("Center");
​
​
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        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);
    }
}

你可能感兴趣的:(GUI编程,java)