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.
发布了56 篇原创文章 · 获赞 2 · 访问量 474

猜你喜欢

转载自blog.csdn.net/jarvan5/article/details/105599938