GUI编程(一)

组件:

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

一.简介

Gui的核心技术:Swing ,AWT,界面不美观。需要JRE环境。

二.AWT

2.1AWT介绍

  1. 包含很多类和接口!GUI:图形用户界面编程
  2. 元素:窗口,按钮,文本框
  3. java.awt
    在这里插入图片描述

2.2组件和容器

1.Frame

package com.kuang.lesson1;

import java.awt.*;

//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(25, 254, 0, 255));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

运行结果:

在这里插入图片描述

问题:发现窗口关闭不掉,停止Java程序才可关闭!

多个窗口:

package com.kuang.lesson1;

import java.awt.*;

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));
        //需要设置可见性   w  h
        setVisible(true);
        //设置大小
        setBounds(x,y,w,h);
        //设置颜色
        setBackground(color);
    }
}

结果显示:

在这里插入图片描述

2.3面板panel

解决了关闭事件

package com.kuang.lesson1;

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

//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(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭时需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

2.4布局管理器

流式布局:从左往右

package com.kuang.lesson1;

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.kuang.lesson1;

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

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.setVisible(true);
        frame.setSize(200,200);
        //添加监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }) ;
    }
}

在这里插入图片描述

表格布局:Grid

package com.kuang.lesson1;

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

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

        //Java函数!自动布局
        frame.pack();
        frame.setVisible(true);

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

在这里插入图片描述

课堂练习:

1.frame窗口,最大的

2.4个面板

上半层: border 一个面板

​ 左:button

​ 中:面板

​ 右:button

下半层: border 一个面板

​ 左:button

​ 中:面板

​ 右:button

在这里插入图片描述

package com.kuang.lesson1;

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

public class TestDemo {
    public static void main(String[] args) {
        //frame需要布局
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));//两行一列
        //四个面板panel
        Panel p1 = new Panel(new BorderLayout());//东西南北中
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        //上半部搞定
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2,BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);
        //中间四个
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i));
        }
        p3.add(p4,BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);

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

在这里插入图片描述

总结:

1.frame是一个顶级窗口

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

3.布局管理器:流式布局,东西南北中,表格

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

2.5事件监听

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

package com.kuang.lesson2;

import javax.swing.*;
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 TestAction {
    public static void main(String[] args) {
        //按下按钮触发一些事件
        Frame frame = new Frame();
        Button button = new Button("hello");

        //因为addActionListener需要一个ActionListener,所以我们需要构造一个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{
    public void actionPerformed(ActionEvent e){
        System.out.println("aaa");
    }
}

多个按钮共享一个事件:

package com.kuang.lesson2;

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

public class TestAction02 {
    public static void main(String[] args) {
        //两个按钮实现同一个监听
        //开始        停止
        Frame frame = new Frame("开始        停止");
        Button button1 = new Button("start");
        Button button2 = new Button("end");
        //可以显示的定义出发会返回的命令,如果不现实点工艺,则会走默认的值!
        //可以多个按钮只写一个监听类
        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);
    }
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
}
class Mymonitor implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //e.getActionCommand() 获得按钮的信息
        System.out.println("按钮被点击了"+e.getActionCommand());

    }
}

2.6输入框TextField监听

package com.kuang.lesson2;

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

public class TestTest01 {
    public static void main(String[] args) {
        //启动!
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();//单排文本框
        add(textField);

        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter,就会触发这个输入框事件
        textField.addActionListener(myActionListener2);

        //设置替换编码
        textField.setEchoChar('*');

        pack();
        setVisible(true);
    }
}
class MyActionListener2 implements ActionListener{
    public void actionPerformed(ActionEvent e){
        TextField field = (TextField) e.getSource();//获得一些资源,返回了一个对象
        System.out.println(field.getText());//获得输入框文本
        field.setText("");//null ""
    }
}

2.7简易计算器,组合+内部类

oop原则:组合,大于继承!

class A extends B{
    
}
class A{
    public B b;
}

加法计算器demo01

package com.kuang.lesson2;

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
    public Calculator(){
        //3个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);//字符数
        TextField num3 = new TextField(20);//字符数

        //1个按钮
        Button button = new Button("=");
        //button需要增加一个监听事件
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));
        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    private  TextField num1,num2,num3;
    public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得两个加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        //2.将这个值加法运算后,放到第三个框
        num3.setText(""+(n1+n2));
        //3.清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

完全改造为面向对象写法:

package com.kuang.lesson2;

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

public class TestCal02 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}
//计算器类
class Calculator1 extends Frame {
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //1个按钮
        Button button = new Button("=");
        //button需要增加一个监听事件
        button.addActionListener(new MyCalculatorListener1(this));
        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener1 implements ActionListener {
    //获取计算器这个变量,在一个类中组合另外一个类;
    Calculator1 calculator1 = null;
    public MyCalculatorListener1( Calculator1 calculator1) {
        this.calculator1 = calculator1;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得两个加数
        int n1 = Integer.parseInt(calculator1.num1.getText());
        int n2 = Integer.parseInt(calculator1.num2.getText());
        //2.将这个值加法运算后,放到第三个框
        calculator1.num3.setText(""+(n1+n2));
        //3.清除前两个框
        calculator1.num1.setText("");
        calculator1.num2.setText("");
    }
}

内部类:更好的包装

package com.kuang.lesson2;

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

public class TestCal02 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}
//计算器类
class Calculator1 extends Frame {
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //1个按钮
        Button button = new Button("=");
        //button需要增加一个监听事件
        button.addActionListener(new MyCalculatorListener1());
        //1个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //监听器类
    //内部类最大的好处就是畅通无阻的访问外部类属性
    private class MyCalculatorListener1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得两个加数
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            //2.将这个值加法运算后,放到第三个框
            num3.setText(""+(n1+n2));
            //3.清除前两个框
            num1.setText("");
            num2.setText("");
        }
    }
}

2.8画笔

package com.kuang.lesson03;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new Mypaint().loadFrame();
    }
}
class Mypaint extends Frame{
    public  void loadFrame(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        //画笔,需要有颜色,画笔可以画画
        //g.setColor(Color.red);
        //g.drawOval(100,100,100,100);
        g.fillOval(100,100,100,100);//实心的圆
        //g.setColor(Color.GREEN);
        g.fillRect(150,200,200,200);
        //养成习惯,画笔用完,将他还原为最初的颜色
    }
}

2.9鼠标监听

目的:想要实现鼠标画画!

package com.kuang.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

//测试鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new Myframe("画画");
    }
}
//自己的类
class Myframe extends Frame{
    //画画需要画笔,需要监听鼠标当前位置,需要集合来存储这个点
    ArrayList points;
    public Myframe(String title){
        super(title);
        setBounds(200,200,400,300);
        //存鼠标的点
        points = new ArrayList<>();
        setVisible(true);
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());
    }
    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标事件
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
            Point point = (Point)iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }

    //适配器模式
    private class MyMouseListener extends MouseAdapter {
        //鼠标 按下,谈起,按住不放

        @Override
        public void mousePressed(MouseEvent e) {
           Myframe myframe =(Myframe)e.getSource();
           //这里我们点击的时候就会在界面上产生一个点!
           //这个点就是鼠标的点
           myframe.addPaint( new Point(e.getX(),e.getY()));
           //每次点击鼠标都需要重新画一遍
            myframe.repaint();//刷新
        }
    }
}

在这里插入图片描述

2.10 窗口监听

package com.kuang.lesson03;

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

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame(){
        setBounds(100,100,200,200);
        setBackground(Color.blue);
        setVisible(true);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(new WindowAdapter() {//匿名内部类

            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("windowopen");
            }
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowclosing");
                System.exit(0);
            }
            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("windowclosed");
            }
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame source = (WindowFrame) e.getSource();
                source.setTitle("被激活了!");
                System.out.println("windowActivated");
            }
        });
    }
}

2.11键盘监听

package com.kuang.lesson03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

//键盘
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前的码
               int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性VK_xxx就可以
                System.out.println(keyCode);
               if(keyCode == KeyEvent.VK_UP){
                   System.out.println("你按下了上键");
               }
               //根据按下不同操作,产生不同结果
            }
        });
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值