相关文章推荐
闯红灯的牛肉面  ·  webRequest.onBeforeReq ...·  2 周前    · 
不羁的生姜  ·  【LuKS】Vba if not c is ...·  1 年前    · 
打篮球的汽水  ·  PyTorch 错误 ...·  1 年前    · 
千杯不醉的生菜  ·  Create a New SAML ...·  2 年前    · 

ActionListener动作事件监听器,当你在点击按钮时希望可以实现一个操作就得用到该接口了。

ActionListener接口所在包

ActionListener接口在event包中,即在开头引入该包。

import java.awt.event.*;

ActionListener接口使用方法

该接口只用实现一个方法叫做actionPerformed(ActionEvent arg0)这个方法。这个方法就是你希望触发事件时程序要做什么。

class ButtonListener/*这里你可以改名字*/ implements ActionListener {
	public void actionPerformed(ActionEvent arg0) {
        /*content*/

但如果只写这一个ButtonListener类我们发现是无法在点击按钮时运行该方法的。呵呵,你还没有给按钮添加这个对象呢。记得要给按钮添加一个ActionListener的对象,即写如下代码。

ButtonListener button_listener = new ButtonListener();
button.addActionListener(button_listener);

接下来如果你又想移除该对象了,就直接remove掉就行了

button.removeActionListener(button_listener);

最后再唠叨一句,ActionListener接口不仅仅适用与点击按钮时触发事件,还可以在文本框、密码框按回车时触发事件等等。

package technology;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstActionListener extends JFrame {
	final static long serialVersionUID = 1L;
	Container container = getContentPane();
	JButton button = new JButton("点击我");
	class ButtonListener implements ActionListener {
		int x = 0;
		public void actionPerformed(ActionEvent arg0) {
			MyFirstActionListener.this.button.setText("我被点机了" + (++x) + "次");
	public MyFirstActionListener()
		super("JFrame窗体");
		this.setBounds(200, 100, 200, 200);
		button.addActionListener(new ButtonListener());
		container.add(button);
		this.setVisible(true);
	public static void main(String[] args)
		new MyFirstActionListener();

效果图如下:

文章目录1、什么是ActionListener1.1 了解ActionEvent类2、ActionListener作用3、ActionListener用法 1、什么是ActionListener 与KeyListener相似,ActionListener也是一个继承了Event接口接口,我们需要一个类来实现ActionListener接口或者继承一个实现了ActionListener接口的类,...
消费者的链接可以使用c1.andThen(c2).accept(x)完成 不返回数据 BiConsumer: BiConsumer <整数,整数> con1 =(a,b)-> System.out.println(a + b);
事件处理机制的分析 swing的事件处理机制其实就是一个委托事件处理,是由事件源(组件)、事件(Event)、监听器(Listener)和 事件处理器(引发事件后的处理方式)组成。 产生事件的组件就叫事件源 事件是一个操作(通常使用各种输入设备,如键盘、鼠标等来完成)。 包含事件处理,并负责检查事件是否发生,若发生就激活事件处理器进行处理的类叫做事件监听类,其实例就是事件监听对象。事件监听器必须实现事件监听器接口或者继承事件监听器适配类。 事件处理器 事件处理器是一个.
/*class LabelListener implements ActionListener{ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub label.setTex... λ表达式的目标类型是“函数接口(functional interface)”,这是Java8新引入的概念。 它的定义是:一个接口,如果只有一个显式声明的抽象方法,那么它就是一个函数接口。一般用@FunctionalInterface标注出来(也可以不标)。 @FunctionalInterface public interface Runnable run(); public interface Callable<V> call() throws Exception; public interface ActionListener actionPerformed(ActionEvent public interface Comparator<T> compare(T boolean equals(Object obj); 注意最后这个
1、 自定义类说明: (1)类名:MainTest 作用:主类,构建主界面 继承的父类:Jframe 主要实现接口ActionListener 主要成员变量: private JPanel ContentPane; private JButton Insert = new JButton(); private JButton Select = new JButton(); private JButton Update = new JButton(); 内部类:MainTest_actionPerformed,主页面的事件监听。 jenter_ actionPerformed,按钮的事件监听。 主要成员方法: public MainTest() public static void main(String[] args) (2)类名:Insert 作用:实现添加个人信息 继承的父类:Jframe 主要实现接口ActionListener 主要成员变量: private JPanel ContentPane; private JLabel jlab1 = new JLabel(); private JLabel jlab2 = new JLabel(); private JLabel jlab3 = new JLabel(); private JLabel jlab4 = new JLabel(); private JLabel jlab5 = new JLabel(); private JLabel jlab6= new JLabel(); private JLabel jlab7 = new JLabel(); private JLabel jlab8 = new JLabel(); private JTextField jtextfield1 = new JTextField(); private JTextField jtextfield2 = new JTextField(); private JTextField jtextfield3 = new JTextField(); private JTextField jtextfield4 = new JTextField(); private JTextField jtextfield5 = new JTextField(); private JComboBox jcombobox1 = new JComboBox(); private JComboBox jcombobox2 = new JComboBox(); private JTextArea jtextarea = new JTextArea(); private JButton sure = new JButton(); 内部类:sure_actionperformed,按钮的事件监听,实现对数据的插入。 主要成员方法:public Insert(); (3)类名:Update 作用:对个人信息进行修改。 继承的父类:Jframe 主要实现接口ActionListener 主要成员变量: private JPanel ContentPane; private JLabel jlab1 = new JLabel(); private JLabel jlab2 = new JLabel(); private JLabel jlab3 = new JLabel(); private JLabel jlab4 = new JLabel(); private JLabel jlab5 = new JLabel(); private JTextField jtextfield1 = new JTextField(); private JTextField jtextfield2 = new JTextField(); private JTextField jtextfield3 = new JTextField(); private JTextField jtextfield4 = new JTextField(); private JButton sure = new JButton(); private JButton upda = new JButton(); 主要成员方法: public Update() public void actionPerformed(ActionEvent e) (4)类名:Select 作用:查询学生个人信息 继承的父类:Jframe 主要实现接口ActionListener 主要成员变量: private JTextArea taInfo=new JTextArea(); private JPanel panel=new JPanel(); private JTextField jtextfield1=new JTextField(); private JButton jbutton=new JButton("查询"); private JLabel jlabel=new JLabel("学号"); 主要成员方法: public Select() public void actionPerformed(ActionEvent e)
1.按钮的事件通过监听按钮接口,是不是,监听就是解释你的动作呢?按下,释放,按下不放代表什么“意义”。 2.事件来源:用Java语言来解释,就是可以把用户操作(鼠标,键盘,关闭窗口)变成事件的对象。 3.事件:用对象来表示,在java.awt.event会有很多事件的类Mouseevent,keyevent,windowsevent. 4.监听:当事件来源发出动作变为事件对象,程序大多时候都是
一、匿名监听器 监听器类是特意为创建一个GUI组件(例如,一个按钮)而设计的监听对象。监听器类不被其他应用程序所共享,因此,正确的做法是将它作为一个内部类定义在框架中。 可以使用匿名内部类简化内部类监听器。匿名内部类时没有名字的内部类。它进一步完成定义内部类和创建一个该类的实例。 内部类EnlargeListener:public ControlCircle2(){ //omitted