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) {
          
   }

}

输出:

经验分享 程序员 微信小程序 职场和发展