Java按钮监听器ActionListener 事件监听教程.

本文详细介绍了在Java GUI中如何创建按钮的事件监听器,包括自定义监听类、实现ActionListener接口、重写actionPerformed方法及如何将监听器添加到按钮上。通过实例演示了两个按钮共享同一监听器的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按钮点击产生的效果通过事件监听来实现,下面介绍如何创建一个按钮的监听器,

一、创建监听器

1. 创建一个普通的Frame和然后添加一个按钮,参考教程
2.自制一个MyActionListener 的监听类

2.1 MyActionListener 需要实现implements接口ActionListener

public class MyActionListener implements ActionListener

2.2 重写唯一的actionPersformed(ActionEvent e)方法,
2.3 e.getActionCommand() 返回的是e 的触发事件的动作命令 (idea中ctrl+左键)来查看源码

代码如下
package GUI.事件监听;

import GUI.MyClass.MySystemExit;

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

public class Test2两个按钮实现同一个监听 {
    public static void main(String[] args) {
        Frame frame = new Frame("Test2两个按钮实现同一个监听");
        frame.setVisible(true);
        Button north = new Button("north");
        Button south = new Button("south");
        //自定义触发会显示的ActionCommand 默认 为Button("...");中的值.
        //add listener for the south and north...
        north.addActionListener(new MyMonitor());
        south.addActionListener(new MyMonitor());


        //2个按钮add the same ActionListener
        frame.add(north, BorderLayout.NORTH);
        frame.add(south, BorderLayout.SOUTH);

        //l,s,c
        frame.setLocation(100, 100);
        frame.setSize(400, 400);
        frame.setBackground(new Color(99, 255, 240));
        //System.exit(0);
        new MySystemExit(frame);


    }

    private static class MyMonitor implements ActionListener {
        //build the ActionLister for the north button and the south button ,named myActionListener

        @Override
        public void actionPerformed(ActionEvent e) {
            //输入 e. 查看源码.
            if (e.getActionCommand() == "north") {
                System.out.println("north Button been clicked ,and MyMonitor class run successfully.");
            } else if (e.getActionCommand() == "south") {
                System.out.println("south Button been clicked ,and MyMonitor class run successfully.");
            }
        }
    }


}
3.新建事件对象,将其添加到按钮中.
   MyActionListener myActionListener = new MyActionListener();
   north.addActionListener(myActionListener);
   south.addActionListener(myActionListener);

总代码, Demo.java

package GUI.事件监听;

import GUI.MyClass.MyActionListener;
import GUI.MyClass.MySystemExit;

import java.awt.*;

/**
* 自制一个事件监听类,添加到按钮里。
*/
public class Demo {
 public static void main(String[] args) {
     Frame frame = new Frame("事件监听");
     Button north = new Button("north");
     Button south = new Button("south");
     //因为addActionListener(...) 需要一个 ActionListener 所以我门自己建造一个 MyActionListener
     //查看源码得知,public synchronized void addActionListener(ActionListener l)
     MyActionListener myActionListener = new MyActionListener();
     north.addActionListener(myActionListener);
     south.addActionListener(myActionListener);

     frame.add(north,BorderLayout.NORTH);
     frame.add(south,BorderLayout.SOUTH);
     frame.pack();//打包.pack()

     //color location size
     frame.setLocation(100,100);
     frame.setSize(400,400);
     frame.setBackground(new Color(99, 255, 240));
     //可见性.
     frame.setVisible(true);
     //add ActionListener.
     new MySystemExit(frame);

 }
}

执行窗口退出的类 MySystemExit.java

package GUI.MyClass;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MySystemExit {
    public MySystemExit(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("System.exit(0)");
                System.exit(0);

            }
        });
    }
}

GUI效果

在这里插入图片描述

点击效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JarvanStack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值