Title

GUI编程

GUI编程

我们要怎么学?

  • 是什么,怎么玩,怎么用

    组件

  • 窗口

  • 弹窗

  • 面板

  • 文本框

  • 列表框

  • 按钮

  • 图片

  • 监听事件

  • 键盘事件

  • 破解工具

1.简介

GUI的核心技术:Swing AWT

  1. 界面不美观
  2. 需要JRE

为什么要学习?

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

2.AWT

2.1 Awt介绍

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

2.2 组件和容器

1. Frame

package com.teatea.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {

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

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

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

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

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

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

问题:窗口关不了,需要通过停止程序运行

=====================================================================

尝试回顾封装:

package com.teatea.lesson01;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        new MyFrame(100,100,200,200,Color.blue);
        new MyFrame(300,100,200,200,Color.yellow);
        new MyFrame(100,300,200,200,Color.red);
        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);
        setBounds(x,y,w,h);
        setVisible(true);

    }
}

=====================================================================

2. 面板Panel

解决关闭窗口事件

package com.teatea.lesson01;

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

//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(40,161,35));

        //panel 设置坐标,相对于Frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(193,15,60));

        //frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

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

    }
}

=====================================================================

3. 布局管理器

  • 流式布局
package com.teatea.lesson01;

import java.awt.*;

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

        frame.setSize(200,200);

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

        frame.setVisible(true);
    }
}

  • 东西南北中
package com.teatea.lesson01;

import java.awt.*;

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

  • 表格布局
package com.teatea.lesson01;

import java.awt.*;

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

=====================================================================

思考题

写出图样式的button按钮布局

思路分析过程:

代码演示:

//未完待续package com.teatea.lesson01;

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

public class Test {
    public static void main(String[] args) {
        //总Frame
        Frame frame = new Frame("Test");

        frame.setSize(400,300);
        frame.setLocation(900,400);
        frame.setBackground(new Color(193,23,66));
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));

        //4个面板
        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("p4-btn"+i));
        }
        panel3.add(panel4,BorderLayout.CENTER);

        frame.add(panel1);
        frame.add(panel3);


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

    }
}

效果图:

小结

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

=====================================================================

4. 事件监听

事件监听:当某个事情发生的时候,干什么?

package com.teatea.lesson02;

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

        //因为addActionListener()需要一个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");
    }
}

多个按钮共享一个事件

package com.teatea.lesson02;

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

public class TestActionTwo {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始   停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

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

        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }
}

//写一个类就可以了
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommond()获得按钮的信息
        System.out.println("按钮被点击了:msg=>"+e.getActionCommand());

    }
}

5. 输入框TextField监听

//跳过啦,有空再补充
posted @ 2022-10-03 17:43  花里胡哨的渣渣  阅读(14)  评论(0)    收藏  举报