java 实现两个按钮实现同一个监听教程

使用e.getActionCommand() 获得事件的ActionCommand 然后根据 ActionCommand 执行对应的输出,

一、教程

1.新建一个普通的有2个按钮的的Frame窗口,参考点击跳转
2.新建一个可以根据不同的ActionCommand执行不同语句的类
    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.将2 的类的对象添加到button中
     north.addActionListener(new MyMonitor());
     south.addActionListener(new MyMonitor());

总代码. Test2两个按钮实现同一个监听.java

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.");
            }
        }
    }


}

实现窗口关闭的类 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);

            }
        });
    }
}
效果图

在这里插入图片描述

打印的输出

south Button been clicked ,and MyMonitor class run successfully.
north Button been clicked ,and MyMonitor class run successfully.
south Button been clicked ,and MyMonitor class run successfully.
north Button been clicked ,and MyMonitor class run successfully.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JarvanStack

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

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

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

打赏作者

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

抵扣说明:

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

余额充值