GUI编程(一)

1. 简介

Gui的核心技术:

  • Swing AWT

为什么市面很少使用GUI技术?

  • 因为界面不美观。
  • 几 M 的小工具,需要安装很大的jre环境!

为什么我们要学习?

  1. 可以写出自己心中想要的一些小工具,比如:贪吃蛇小游戏。
  2. 工作时候,有可能需要维护到swing界面,概率极小!
  3. 了解MVC架构,了解监听!

2. 常用组件

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

3. AWT

3.1AWT介绍

img

3.2组件和容器

窗口

package com.yyh.gui.frame;

import java.awt.*;

/**
 * Gui-frame窗口学习
 * @author yyh
 * @create 2021-10-29 16:28
 **/
public class TestFrame01 {
    public static void main(String[] args) {
        //窗口
        Frame frame = new Frame("yyh");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(500,500);
        //设置颜色
        frame.setBackground(new Color(51, 146, 232));
        //弹出初始位置
        frame.setLocation(300,300);
        //设置大小是否固定,默认为:true-不固定
        frame.setResizable(true);
    }

}

窗口无法关闭;停止java程序运行

窗口
  • 多个窗口
package com.yyh.gui.frame;

import java.awt.*;

/**
 * 测试多个窗口
 * @author yyh
 * @create 2021-10-29 16:39
 **/
public class TestFrame02 {
    //同时展示多个窗口
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200,Color.cyan);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200,Color.green);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200,Color.red);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200,Color.yellow);
    }

    static class MyFrame extends Frame{
        //可能存在多个窗口,我需要一个计数器来进行计数
        static int id = 0;

        public MyFrame(int x,int y,int w,int h,Color color){
            //id自增
            super("yyh+"+(++id));
            //设置可见性
            setVisible(true);
            //设置弹出位置 和 长宽
            setBounds(x,y,w,h);
            //设置背景颜色
            setBackground(color);
        }

    }
}

多窗口

面板

package com.yyh.gui.panel;

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

/**
 * gui-panel面板学习
 * @author yyh
 * @create 2021-10-29 16:52
 **/
public class TestPanel01 {
    //panel 可以看做一个空间,但不能单独存在
    public static void main(String[] args) {
        //布局的概念
        Panel panel = new Panel();
        Frame frame = new Frame();

        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        //背景色
        frame.setBackground(new Color(47, 106, 236));

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

        //panel依托于Frame
        frame.add(panel);
        //设置可见
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件  System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

窗口关闭

//监听事件,监听窗口关闭事件  System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        //结束程序
        System.exit(0);
    }
});
面板

3.3布局管理器

流式布局

package com.yyh.gui.layout;

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

/**
 * 流式布局
 * @author yyh
 * @create 2021-10-29 17:42
 **/
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //坐标
        frame.setBounds(300,300,500,500);
        //背景色
        frame.setBackground(new Color(47, 106, 236));
        //设置按钮
        Button button = new Button("yyh1");
        Button button1 = new Button("yyh2");
        Button button2 = new Button("yyh3");
        //设置为流式布局
        frame.setLayout(new FlowLayout());    //默认居中
        // frame.setLayout(new FlowLayout(FlowLayout.LEFT));  //左
        //添加按钮
        frame.add(button);
        frame.add(button1);
        frame.add(button2);
        //显示
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件  System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

流式布局

东西南北中

package com.yyh.gui.layout;

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

/**
 * 东南西北中布局
 * @author yyh
 * @create 2021-11-02 15:17
 **/
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("yyh");
        //东南西北中布局
        frame.add(new Button("EAST"),BorderLayout.EAST);
        frame.add(new Button("SOUTH"),BorderLayout.SOUTH);
        frame.add(new Button("WEST"),BorderLayout.WEST);
        frame.add(new Button("NORTH"),BorderLayout.NORTH);
        frame.add(new Button("CENTER"),BorderLayout.CENTER);


        //可见
        frame.setVisible(true);
        //自适应大小
        //frame.pack();

        //设置定位和大小
        frame.setBounds(300,300,500,400);
        //添加监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

东南西北中

表格布局

package com.yyh.gui.layout;

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

/**
 * 表格布局
 * @author yyh
 * @create 2021-11-02 15:58
 **/
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("yyh");
        //布局,两列三行
        frame.setLayout(new GridLayout(2,3));
        for (int i = 1; i <= 6; i++) {
            frame.add(new Button(i + ""));
        }
        //设置可见
        frame.setVisible(true);
        //设置定位和大小
        frame.setBounds(300,300,500,400);
        //添加监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭程序
                System.exit(0);
            }
        });
    }
}

表格布局

嵌套布局

package com.yyh.gui.layout;

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

/**
 * 嵌套布局
 * @author yyh
 * @create 2021-11-02 16:16
 **/
public class TestLayout {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame{

    public MyFrame(){
        this.loadMyframe();
    }
    //加载窗口
    public void loadMyframe(){
        //组件
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new BorderLayout());
        Panel p3 = new Panel(new GridLayout(2,1));
        Panel p4 = new Panel(new GridLayout(2,2));
        //布局
        this.setLayout(new GridLayout(2,1));
        //上
        this.add(p1);
        p1.add(new Button("1"),BorderLayout.WEST);
        p1.add(new Button("2"),BorderLayout.EAST);
        p3.add(new Button("p3-1"));
        p3.add(new Button("p3-2"));
        p1.add(p3,BorderLayout.CENTER);
        //下
        this.add(p2);
        p2.add(new Button("3"),BorderLayout.WEST);
        p2.add(new Button("4"),BorderLayout.EAST);
        for (int i = 1; i <= 4; i++) {
            p4.add(new Button("p4-" + i));
        }
        p2.add(p4,BorderLayout.CENTER);
        //设置可见
        this.setVisible(true);
        //设置定位和大小
        this.setBounds(300,300,500,400);
        //设置监听事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
嵌套布局

3.4 事件监听

#### 单按钮事件监听
package com.yyh.gui.listener;

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

/**
 * 单按钮监听事件
 *  @author yyh
 * @create 2021-11-03 18:03
 **/
public class TsetActionEvent {
    public static void main(String[] args) {
        //组件
        Frame frame = new Frame("按钮监听事件");
        Button button = new Button("check!!!");
        //监听
        button.addActionListener(new MyActionEvent());

        frame.add(button);
        //设置可见
        frame.setVisible(true);
        //自适应大小
        frame.pack();
        frame.setBounds(300,300,500,400);
        //退出程序
        closeWindow(frame);
    }

    //退出程序
    private static void closeWindow(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//事件监听类
class MyActionEvent implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮监听事件");
    }
}
按钮监听

多按钮监听同一事件

package com.yyh.gui.listener;

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

/**
 * 多按钮监听同一事件
 * @author yyh
 * @create 2021-11-04 9:39
 **/
public class TsetActionEvent2 {

    public static void main(String[] args) {
        //组件
        Frame frame = new Frame("yyh");
        Button b1 = new Button("begin");
        Button b2 = new Button("end");
        //可以显示的定义触发后返回的命令,如果不显示定义,则会走默认的值!可以多个按钮只写一个监听潴
        b2.setActionCommand("stop");

        //监听
        b1.addActionListener(new MyActionEvent2());
        b2.addActionListener(new MyActionEvent2());

        //布局
        frame.add(b1,BorderLayout.WEST);
        frame.add(b2,BorderLayout.EAST);
        frame.pack();
        frame.setBounds(300,300,500,400);
        frame.setVisible(true);

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

}

//事件监听类
class MyActionEvent2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }
}

多按钮监听同一事件

文本框事件监听

package com.yyh.gui.listener;

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

/**
 * 文本框
 * @author yyh
 * @create 2021-11-04 9:57
 **/
public class Testfield {

    public static void main(String[] args) {
        new MyFrame();
    }

}

class MyFrame extends Frame{
    public MyFrame(){
        TextField field = new TextField();
        this.add(field);

        //监听文本框
        field.addActionListener(new MyAction());

        //设置字体掩码
        field.setEchoChar('*');

        this.setVisible(true);
        this.setBounds(500,300,500,400);

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

}

//监听类
class MyAction implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField)e.getSource();
        System.out.println(field.getText());
        field.setText("");
    }
}

文本框

简易计算器

package com.yyh.gui.listener;

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

/**
 * 计算器
 * @author yyh
 * @create 2021-11-04 10:25
 **/
public class TestCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame{
    //组件
    TextField t1 = new TextField(5);
    TextField t2 = new TextField(5);
    TextField t3 = new TextField(15);
    Button button = new Button(" = ");
    Label label = new Label(" + ");

    public Calculator(){
        //布局
        this.setLayout(new FlowLayout());
        this.add(t1);
        this.add(label);
        this.add(t2);
        this.add(button);
        this.add(t3);

        //监听
        button.addActionListener(new MyAction());

        this.setVisible(true);
        this.setBounds(500,300,500,400);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    //监听器类---》内部类
    //内部类好处,畅通无阻的访问外部类
    private class MyAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(t1.getText());
            int n2 = Integer.parseInt(t2.getText());
            t3.setText(""+(n1+n2));
            t1.setText("");
            t2.setText("");
        }
    }
}
简易计算器

画笔

package com.yyh.gui.listener;

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

/**
 * 画笔
 * @author yyh
 * @create 2021-11-04 10:48
 **/
public class TestPaint {

    public static void main(String[] args) {
        new MyPaint();
    }

}

class MyPaint extends Frame{

    public MyPaint(){
        this.setVisible(true);
        this.setBounds(500,300,500,400);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //画笔
    @Override
    public void paint(Graphics g) {
        //super.paint(graphics);
        //画笔需要有颜色
        g.setColor(Color.red);
        //可以画画
        g.drawOval(100,100,100,100);//空心
        g.fillOval(200,200,100,100);//实心
        g.setColor(Color.green);
        g.fillOval(200,200,100,100);//实心
        //养成习惯,画笔用完将它还原到最初的颜色
    }

}
画笔

鼠标事件监听

package com.yyh.gui.listener;

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

/**
 * 鼠标监听事件
 * @author yyh
 * @create 2021-11-04 11:36
 **/
public class TestMouse {
    public static void main(String[] args) {
        new MyFrame1("画笔");
    }
}

//鼠标类
class MyFrame1 extends Frame {
    //画画需要画笔,需要监听鼠标当前的位置
    //需要集合来存储这个点
    //存鼠标的点
    ArrayList points = new ArrayList<>();

    public MyFrame1(String title) {
        super(title);
        setBounds(200, 200, 400, 400);

        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouse());
        setVisible(true);

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

    @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 addPoint(Point point) {
        points.add(point);
    }

    //适配器模式
    private class MyMouse extends MouseAdapter {
        //鼠标   按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame1 myFrame = (MyFrame1) e.getSource();
            //当我们点击的时候就会在界面上产生一个点
            //这个点就是鼠标的点
            myFrame.addPoint(new Point(e.getX(), e.getY()));
            //每一次点击鼠标都需要重新画一遍
            myFrame.repaint();//刷新
        }
    }
}

鼠标监听

窗口事件监听

package com.yyh.gui.listener;

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

/**
 * 窗口监听
 * @author yyh
 * @create 2021-11-04 15:10
 **/
public class TestWindow {
    public static void main(String[] args) {

        new MyWindow("窗口监听");
    }
}

class MyWindow extends Frame{
    public MyWindow(String title){
        super(title);
        this.setBounds(200, 200, 400, 400);
        this.setVisible(true);
        this.setBackground(Color.cyan);

        this.addWindowListener(new WindowAdapter() {
            //关闭程序
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            //打开窗口
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("打开窗口");
            }

            //关闭窗口
            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("关闭窗口");
            }

            //最小化
            @Override
            public void windowIconified(WindowEvent e) {
                System.out.println("最小化");
            }

            //最大化
            @Override
            public void windowDeiconified(WindowEvent e) {
                System.out.println("最大化");
            }

            //激活窗口
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("激活窗口");
            }

        });
    }
}

窗口监听

键盘事件监听

package com.yyh.gui.listener;

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

/**
 * 键盘监听
 * @author yyh
 * @create 2021-11-04 15:24
 **/
public class TestKey {
    public static void main(String[] args) {
        new MyKeyFrame();
    }
}

class MyKeyFrame extends Frame{
    public MyKeyFrame(){
        this.setBounds(200, 200, 400, 400);
        this.setVisible(true);
        this.setBackground(Color.cyan);

        this.addWindowListener(new WindowAdapter() {
            //关闭程序
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_SPACE){
                    System.out.println("空格--跳!!!");
                }
            }
        });
    }
}

键盘监听

总结:
1.Frame是一个顶级窗口

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

3.布局管理器

  • 流式布局
  • 东西南北中布局
  • 表格布局
  • 嵌套布局

4.组件常用属性:大小,定位,背景颜色,可见性,监听!

5.事件监听

  • 按钮
  • 文本框
  • 鼠标
  • 窗口
  • 键盘

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