前言:

本文内容:事件监听、输入框事件监听、简易计算器和回顾组合内部类、画笔paint、鼠标监听事件和模拟画图工具、窗口监听事件、键盘监听事件

推荐免费JavaGUI入门视频:【狂神说Java】GUI编程入门到游戏实战_哔哩哔哩_bilibili

事件监听

  • 当某个事情发生的时候,干什么?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    package com.jokerdig.lesson2;

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

    /**
    * @author Joker大雄
    * @data 2021/8/17 - 21:14
    **/
    public class TestActionEvent {
    public static void main(String[] args) {
    //按下按钮,触发事件
    Frame frame = new Frame();
    Button button = new Button();
    //因为,addActionListener()需要一个ActionListener,我们需要构造ActionListener
    MyActionListener myActionListener = new MyActionListener();
    button.addActionListener(myActionListener);

    frame.add(button,BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    windowClose(frame);//关闭窗口
    }

    //关闭窗口
    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("按钮点击事件");
    }
    }
  • 让两个按钮共用一个事件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package com.jokerdig.lesson2;

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

    /**
    * @author Joker大雄
    * @data 2021/8/17 - 21:25
    **/
    public class TestActionEventTwo {
    public static void main(String[] args) {
    //两个按钮实现同一个监听
    //开始 停止
    Frame frame =new Frame("开始-停止");
    Button button1 = new Button("start");
    Button button2 = new Button("stop");

    //可以显示的定义触发会返回的命令
    button2.setActionCommand("定义事件");

    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) {
    //
    System.out.println("按钮被点击了:msg"+e.getActionCommand());
    }
    }

输入框事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.jokerdig.lesson2;

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

/**
* @author Joker大雄
* @data 2021/8/17 - 21:39
**/
public class TestText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}

class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 maction = new MyActionListener2();
textField.addActionListener(maction);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}

class MyActionListener2 implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源
TextField text1=(TextField) e.getSource();
text1.getText();//获得输入框文本
System.out.println(text1.getText());
text1.setText("");
}
}

简易计算器、回顾组合内部类

  • opp原则:组合大于继承

    1
    2
    3
    4
    5
    6
    7
    class A extends B{

    }
    class A extends B{
    //A用B功能,把B私有进来,降低耦合
    private B b;
    }

制作简易计算器:

  • 示例图:

    f5xHIO.md.png

  • 效果图

    fIPxyQ.png

  • 代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    package com.jokerdig.lesson2;

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

    /**
    * @author Joker大雄
    * @data 2021/8/17 - 21:58
    **/
    //简易计算器
    public class TestClac {
    public static void main(String[] args) {
    //启动
    new Calculator().loadFrame();
    }
    }
    //计算器类
    class Calculator extends Frame{
    //属性
    TextField text1,text2,text3;
    //方法
    public void loadFrame(){
    Frame f=new Frame("简易计算器");
    //3个文本框
    text1 = new TextField(10);//字符数
    text2 = new TextField(10);//字符数
    text3 = new TextField(20);//字符数

    //1 个按钮
    Button button = new Button("=");
    //按钮监听事件
    button.addActionListener(new MyCalculatorListener(this));
    //1 个标签
    Label label = new Label("+");
    //布局
    f.setLayout(new FlowLayout());//流式布局

    f.add(text1);
    f.add(label);
    f.add(text2);
    f.add(button);
    f.add(text3);
    f.pack();//自适应
    f.setVisible(true);//显示布局
    closeWindow(f);//关闭窗口
    }

    //关闭窗口
    public static void closeWindow(Frame frame){
    frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
    //关闭
    System.exit(0);
    }
    });
    }
    }

    //监听器类
    class MyCalculatorListener implements ActionListener{

    /*用构造器获取三个变量
    private TextField text1,text2,text3;
    */
    //获取计算器对象,在一个类中组合另一个类
    Calculator cal = null;
    public MyCalculatorListener(Calculator cal) {
    this.cal = cal;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    //1.获得加数和被加数
    int t1=Integer.parseInt(cal.text1.getText());
    int t2=Integer.parseInt(cal.text2.getText());
    //2.把值运算后放入第三个框
    cal.text3.setText(""+(t1+t2));
    //3.清楚前两个框
    cal.text1.setText("");
    cal.text2.setText("");
    }
    }

内部类优势:

  • 更好的包装

  • 更畅通的访问外部类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    package com.jokerdig.lesson2;

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

    /**
    * @author Joker大雄
    * @data 2021/8/17 - 21:58
    **/
    //简易计算器
    public class TestClac {
    public static void main(String[] args) {
    //启动
    new Calculator().loadFrame();
    }
    }
    //计算器类
    class Calculator extends Frame{
    //属性
    TextField text1,text2,text3;
    //方法
    public void loadFrame(){
    Frame f=new Frame("简易计算器");
    //3个文本框
    text1 = new TextField(10);//字符数
    text2 = new TextField(10);//字符数
    text3 = new TextField(20);//字符数

    //1 个按钮
    Button button = new Button("=");
    //按钮监听事件
    button.addActionListener(new MyCalculatorListener());
    //1 个标签
    Label label = new Label("+");
    //布局
    f.setLayout(new FlowLayout());//流式布局

    f.add(text1);
    f.add(label);
    f.add(text2);
    f.add(button);
    f.add(text3);
    f.pack();//自适应
    f.setVisible(true);//显示布局
    closeWindow(f);//关闭窗口
    }

    //关闭窗口
    public static void closeWindow(Frame frame){
    frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
    //关闭
    System.exit(0);
    }
    });
    }
    //监听器类
    //使用内部类简化代码
    private class MyCalculatorListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
    //1.获得加数和被加数
    int t1=Integer.parseInt(text1.getText());
    int t2=Integer.parseInt(text2.getText());
    //2.把值运算后放入第三个框
    text3.setText(""+(t1+t2));
    //3.清楚前两个框
    text1.setText("");
    text2.setText("");
    }
    }
    }

画笔Paint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.lesson3;

import java.awt.*;

/**
* @author Joker大雄
* @data 2021/8/17 - 22:45
**/
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
//画笔类
class MyPaint extends Frame{

public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}

//画笔
public void paint(Graphics g){
//画笔需要颜色,可以绘画
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.setColor(Color.green);
g.fillRect(200,200,100,100);
//养成习惯,画笔用完,还原到最初哟啊色
}
}

鼠标监听事件、模拟画图工具

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

  • 流程图

    fIuuvt.png

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    package com.jokerdig.lesson3;

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

    /**
    * @author Joker大雄
    * @data 2021/8/17 - 23:06
    **/
    public class TestMouseListener {
    public static void main(String[] args) {
    new MyFrame("画图");
    }

    }
    //自己的类
    class MyFrame extends Frame {
    //创建ArrayList
    ArrayList points;
    //绘画需要监听鼠标位置

    //构造器
    public MyFrame(String title) {
    super(title);
    setBounds(200,200,300,300);

    //用数组存放鼠标的点
    points = new ArrayList<>();

    setVisible(true);

    //鼠标监听器,正对这个窗口
    this.addMouseListener(new MyMouseListener());
    }
    @Override
    public void paint(Graphics g){
    //画画需要监听鼠标,使用迭代器输出点Iterator
    Iterator iterator = points.iterator();
    while(iterator.hasNext()){
    Point point = (Point)iterator.next();
    g.setColor(Color.BLACK);
    g.fillOval(point.x,point.y,10,10);
    }
    }

    //添加一个点到界面
    public void addPoint(Point point){
    points.add(point);
    }

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

    //按压
    @Override
    public void mousePressed(MouseEvent e) {
    MyFrame frame=(MyFrame)e.getSource();
    //点击产生一个点,获取鼠标坐标画下点
    frame.addPoint(new Point(e.getX(),e.getY()));

    //每次点击重画一次
    frame.repaint();//刷新
    }
    }
    }

窗口监听事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.jokerdig.lesson3;

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

/**
* @author Joker大雄
* @data 2021/8/17 - 23:28
**/
public class TestWindow {
public static void main(String[] args) {
new windowFrame();
}
}
class windowFrame extends Frame{

public windowFrame(){
setBackground(Color.BLUE);
setBounds(100,100,200,200);
setVisible(true);
// addWindowListener(new MyWindowListener());
//匿名内部类
this.addWindowListener(new WindowAdapter() {



@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}

@Override
public void windowActivated(WindowEvent e) {
windowFrame f = (windowFrame) e.getSource();
f.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
//继承实现类,来避免实现接口重写太多方法
class MyWindowListener extends WindowAdapter{

@Override
public void windowClosing(WindowEvent e) {
//隐藏窗口
setVisible(false);

//关闭窗口
System.exit(0);
}
}

}

键盘监听事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.jokerdig.lesson3;

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

/**
* @author Joker大雄
* @data 2021/8/17 - 23:43
**/
//键盘
public class TestKeyboardListener {
public static void main(String[] args) {
new keyFrame();
}
}
class keyFrame extends Frame{
public keyFrame(){
setBounds(1,2,500,300);
setVisible(true);

this.addKeyListener(new KeyAdapter() {
//键盘按压
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键
int keycode = e.getKeyCode();
System.out.println(keycode);
//不需要可以去记录,使用VK_XX
if(keycode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}