事件监听及示例

本文介绍了Java中如何使用ActionListener监听按钮和文本框事件。通过示例代码展示了如何创建监听器,实现不同按钮和文本框的响应,以及如何获取和处理事件触发时的动作命令。在文本框监听中,还演示了当按下回车键时如何获取并清除文本框内容。
摘要由CSDN通过智能技术生成

按钮的监听事件

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 ActionListenerTest1 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();
        /*
        addActionListener()这个函数需要传入一个ActionListener
        这个ActionListener是继承了EventListener的一个接口
        ActionListener里只有一个方法actionPerformed(ActionEvent e)
        所以我们自己写一个该接口的实现类,并传入addActionListener()
         */
        button.addActionListener(new MyActionListener());
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
        windowClose(frame);
    }

    //把关闭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("按钮被点击了一次");
    }
}

运行结果为:
点击一次frame中的button后,控制台打印一句“按钮被点击了一次”
在这里插入图片描述

一个类监听多个事件

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

public class ActionListenerTest2 {
    public static void main(String[] args) {
        Frame frame = new Frame("开始/停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //为按钮设置动作命令
        button1.setActionCommand("start");
        button2.setActionCommand("stop");
        MyActionListener2 myActionListener = new MyActionListener2();
        //为按钮添加监听类
        button1.addActionListener(myActionListener);
        button2.addActionListener(myActionListener);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}
//用同一个类去监听两个事件
class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()用于获取按钮设置好的动作命令
        System.out.println(e.getActionCommand());
    }
}

运行的结果为:
点击start按钮,控制台打印start;点击stop按钮,控制台打印stop;
在这里插入图片描述

文本框监听事件

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

public class TextActionListener {
    public static void main(String[] args) {
        MyFrame1 myFrame1 = new MyFrame1();
    }
}

//把frame的构建拆出来写成一个类,main方法里只负责启动程序,不再写这些样式等等
class MyFrame1 extends Frame{
    //重写MyFrame1的构造方法
    public MyFrame1(){
        TextField textField = new TextField();
        /*
        MyFrame继承了Frame
        所以在给frame添加textfield时不需要再实例化对象以及frame.add
         */
        add(textField);
        //监听文本框
        MyActionListener3 myActionListener3 = new MyActionListener3();
        //按下enter就会触发这个文本框的事件
        textField.addActionListener(myActionListener3);
        //为了实现输入密码时不可见,设置替换编码
        textField.setEchoChar('*');
        pack();
        setVisible(true);
    }
}
//创建一个用于监听的类
class MyActionListener3 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        /*
        e.getSource()这个函数返回值是一个object,可以向下转型成任何类型
        所以这里转型为text类型
         */
        e.getSource();//获得一些资源
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText());//得到文本框中的内容并打印
        textField.setText("");//为了使输入完成按下回车后文本框内容清空
    }
}

运行结果为:
在文本框内输入“内容”二字,显示为**,按下回车后,控制台打印“内容”二字,文本框清空
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值