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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章