02 事件监听

事件监听:事情发生的时候干什么。

ActionListener

1、监视按钮

1-1 一个按钮实现一个监听

package com.liudr.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 Test01ActionEvent {
    public static void main(String[] args) {
        //按下按钮时触发一些事件
        Frame frame = new Frame();
        frame.setBounds(100,100,100,100);
        Button button = new Button("button");

        //因为,addActionListener()需要一个 ActionListioner,所以我们需要 ActionListioner
        MyActionListener ml = new MyActionListener();
        button.addActionListener(ml);

        frame.add(button);
        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("aaa");
        System.exit(0);
    }
}

1-2 两个按钮实现一个监听

package com.liudr.lesson02;

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

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

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

        MyMonitor mm = new MyMonitor();
        button1.addActionListener(mm);
        button2.addActionListener(mm);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.setVisible(true);

    }
}

class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        System.out.println("按钮被点击了:msg"+e.getActionCommand());
        e.getActionCommand();
    }
}

可以给按钮添加ActionCommand来实现。

2 输入框监听

package com.liudr.lesson02;

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

public class Test03Text {
    public static void main(String[] args) {
        //mian方法只管启动
        new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame () {
        TextField textField = new TextField();
        add(textField);//继承后可直接使用其方法

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

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

        setVisible(true);
        pack();
    }
}

class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//e.getSource获得一些资源,返回一个对象
        System.out.println(field.getText());//获得输入框中的文本
        field.setText("");//设置文本为空
    }
}

3 计算器

3-1 初步

package com.liudr.lesson02;

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

//简易计算器
public class Test04Calculator {
    public static void main(String[] args) {
        new Calculater();
    }
}

//计算器类
class Calculater extends Frame {
    public Calculater() {
       //三个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));

        //一个标签
        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("");
    }
}

2-1 初步优化

实现组合

package com.liudr.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 Text04CalculatorPro {
    public static void main(String[] args) {
        new CalculatorPro();
    }

}

//计算器类
class CalculatorPro extends Frame {
    public TextField num1,num2,num3;
    public CalculatorPro(){
        //创建组件
        this.num1 = new TextField(10);
        this.num2 = new TextField(10);
        this.num3 = new TextField(20);

        Label label = new Label("+");

        Button button = new Button("=");
        button.addActionListener(new CalculatorProListener(this) );

        //设置布局可见性
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

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

    }
}

//计算器监听器
class CalculatorProListener implements ActionListener {
    private CalculatorPro calculatorPro = null;

    public CalculatorProListener(CalculatorPro calculatorPro) {
        this.calculatorPro = calculatorPro;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1、获取num1,num2的数值
        int n1 = Integer.parseInt(calculatorPro.num1.getText());
        int n2 = Integer.parseInt(calculatorPro.num2.getText());

        //2、将这个值加法运算后放到第三个框中。
        calculatorPro.num3.setText(""+(n1+n2));

        //3、清空前两个文本框
        calculatorPro.num1.setText("");
        calculatorPro.num2.setText("");

    }
}

3-3 最终优化

完全对象化,内部类

package com.liudr.lesson02;

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

public class Test04CalculatorPerfact {
    public static void main(String[] args) {
        new CalculatorPerfact().loadFrame();
    }


}
class CalculatorPerfact extends Frame {
    public TextField num1,num2,num3;
    public void loadFrame (){
        //组件
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        button.addActionListener(new PerfectListener());
        Label label = new Label("+");

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



    }

    private class PerfectListener implements ActionListener {


        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");

        }
    }
}

ss

posted on 2020-10-18 13:25  dulling缘  阅读(46)  评论(0)    收藏  举报

导航