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
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
package cn.tedu.chart2; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 客户端窗体 */ public class ClientFrame extends JFrame implements ActionListener, KeyListener { JButton but; JTextArea message; JTextField text; Socket s; ClientThread client; public static void main(String[] args) { new ClientFrame(); } // 在构造函数中对窗体进行初始化 public ClientFrame() { // 取消JFrame的布局 this.setLayout(null); this.setTitle("客户端"); this.setBounds(100, 20, 400, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 聊天记录 --- JTextArea message = new JTextArea(); message.setBounds(10, 10, 360, 200); // 设置聊天记录不能修改 message.setEditable(false); this.add(message); // 信息发送 JTextField JButton text = new JTextField(); text.setBounds(10, 260, 250, 40); text.addKeyListener(this); this.add(text); but = new JButton("发送"); but.setBounds(280, 260, 100, 40); // 给but按钮添加动作监听 but.addActionListener(this); this.add(but); this.setVisible(true); // 和服务器取得联系 try { s = new Socket("127.0.0.1", 65000); client = new ClientThread(s, message); client.start(); } catch (Exception e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == but) { if (!s.isClosed()) { String data = text.getText(); if (!"".equals(data)) { // 向服务器发送信息 client.send(data); // 清空文本框 text.setText(null); } }else { //在关闭状态下点击发送按钮 JOptionPane.showMessageDialog(null, "Socket连接已关闭,不能发送信息!", "提示", JOptionPane.ERROR_MESSAGE); } } } public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER) { if (!s.isClosed()) { String data = text.getText(); if (!"".equals(data)) { // 向服务器发送信息 client.send(data); // 清空文本框 text.setText(null); } }else { //在关闭状态下点击发送按钮 JOptionPane.showMessageDialog(null, "Socket连接已关闭,不能发送信息!", "提示", JOptionPane.ERROR_MESSAGE); } } } public void keyReleased(KeyEvent e) {} }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值