Java————ActionListener

1、什么是ActionListener

与KeyListener相似,ActionListener也是一个继承了Event接口的接口,我们需要一个类来实现ActionListener接口或者继承一个实现了ActionListener接口的类,然后重写actionPerformed方法

ActionListener源码:

public interface ActionListener extends EventListener {

    /**
     * Invoked when an action occurs.
     */
    public void actionPerformed(ActionEvent e);

}

从源码中我们可以看出,ActionListener中只有一个方法,方法的参数为Action
Event,我们只要在实现ActionListener的类/继承了实现ActionListener的类中重写此方法即可,下面我们来了解一下ActionEvent

1.1 了解ActionEvent类

ActionEvent类是一个语义事件表明一个被定义的组件有行为发生,比如你在一个Button上增加了ActionListener,那么每当这个Button被点击(空格键也行)时,就会调用ActionListener类中重写的actionPerformed方法

2、ActionListener作用

ActionListener只有一个actionPerformed方法,当且注册了ActionListner的组件监听到了动作时,就会调用actionPerformed方法

下面是使用三部曲
(1) Implement the ActionListener interface in the class:

public class ActionListenerExample Implements ActionListener  

(2) Register the component with the Listener:

component.addActionListener(instanceOfListenerclass);

(3) Override the actionPerformed() method:

public void actionPerformed(ActionEvent e){  
           //Write the code here
  }

使用示例:

import java.awt.*;  
import java.awt.event.*;  
//1st step  
public class ActionListenerExample implements ActionListener{  
public static void main(String[] args) {  
    Frame f=new Frame("ActionListener Example");  
    final TextField tf=new TextField();  
    tf.setBounds(50,50, 150,20);  
    Button b=new Button("Click Here");  
    b.setBounds(50,100,60,30);  
    //2nd step  
    b.addActionListener(this);  
    f.add(b);f.add(tf);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
//3rd step  
public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Javatpoint.");  
}  
}  

输出:
在这里插入图片描述

3、ActionListener用法

  • ActionListener实战:
package practice;

import java.awt.*;
import java.awt.event.*;

public class AL extends Frame implements WindowListener,ActionListener {
	TextField text = new TextField(20);
	Button b;
	private int numClicks = 0;

	public static void main(String[] args) {
		AL myWindow = new AL("My first window");
		myWindow.setBounds(400,400,350,100);
		myWindow.setLocationRelativeTo(null);
		myWindow.setVisible(true);
	}

	public AL(String title) {

		super(title);
		setLayout(new FlowLayout());
		addWindowListener(this);
		b = new Button("Click me");
		add(b);
		add(text);
		b.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		numClicks++;
		text.setText("Button Clicked " + numClicks + " times");
	}
	@Override
	public void windowClosing(WindowEvent e) {
		dispose();
		System.exit(0);
	}

	@Override
	public void windowOpened(WindowEvent e) {}
	@Override
	public void windowActivated(WindowEvent e) {}
	@Override
	public void windowIconified(WindowEvent e) {}
	@Override
	public void windowDeiconified(WindowEvent e) {}
	@Override
	public void windowDeactivated(WindowEvent e) {}
	@Override
	public void windowClosed(WindowEvent e) {}

}

输出:
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值