zoukankan      html  css  js  c++  java
  • Java事件监听的四种实现方式

    1.事件对象: 
    一般继承自java.util.EventObject对象,由开发者自行定义.

    2.事件源: 
    就是触发事件的源头,不同的事件源会触发不同的事件类型.

    3.事件监听器: 
    事件监听器负责监听事件源发出的事件.一个事件监听器通常实现java.util.EventListener这个标识接口. 

    其整个处理过程是这样的,事件源可以注册事件监听器对象,并可以向事件监听器对象发送事件对象.事件发生后,事件源将事件对象发给已经注册的所有事件监听器. 
    监听器对象随后会根据事件对象内的相应方法响应这个事件.

    以下为四种实现方式

    Java代码:

    package TT;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.RootPaneContainer;
    /**
     *自身类作为事件监听器 :
    */
    class EventListener1 extends JFrame implements ActionListener {
        private JButton btBlue, btDialog;
    
        public EventListener1() {
            setTitle("Java GUI 事件监听处理");
            setBounds(100, 100, 500, 350);
            setLayout(new FlowLayout());
            btBlue = new JButton("蓝色");
            btDialog = new JButton("弹窗");
    
            // 将按钮添加事件监听器
            btBlue.addActionListener(this);
            btDialog.addActionListener(this);
    
            add(btBlue);
            add(btDialog);
    
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        // ***************************事件处理***************************
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btBlue) {
                Container c = getContentPane();
                c.setBackground(Color.BLUE);
            } else if (e.getSource() == btDialog) {
                JDialog dialog = new JDialog();
                dialog.setBounds(300, 200, 400, 300);
                dialog.setVisible(true);
            }
        }
    
    }
    
    /**
     * Java事件监听处理——内部类处理
     *
     */
    
    class EventListener2 extends JFrame {
        private JButton btBlue, btDialog;
    
        // 构造方法
        public EventListener2() {
            setTitle("Java GUI 事件监听处理");
            setBounds(100, 100, 500, 350);
            setLayout(new FlowLayout());
            btBlue = new JButton("蓝色");
            btDialog = new JButton("弹窗");
            // 添加事件监听器对象(面向对象思想)
            btBlue.addActionListener(new ColorEventListener());
            btDialog.addActionListener(new DialogEventListener());
    
            add(btBlue);
            add(btDialog);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        // 内部类ColorEventListener,实现ActionListener接口
        class ColorEventListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container c = getContentPane();
                c.setBackground(Color.BLUE);
            }
        }
    
        // 内部类DialogEventListener,实现ActionListener接口
        class DialogEventListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog();
                dialog.setBounds(300, 200, 400, 300);
                dialog.setVisible(true);
            }
        }
    
    }
    
    /**
     * Java事件监听处理——匿名内部类处理
     *
     */
    class EventListener3 extends JFrame {
        private JButton btBlue, btDialog;
    
        public EventListener3() {
            setTitle("Java GUI 事件监听处理");
            setBounds(100, 100, 500, 350);
            setLayout(new FlowLayout());
    
            btBlue = new JButton("蓝色");
            btDialog = new JButton("弹窗");
    
            // 添加事件监听器(此处即为匿名类)
            btBlue.addActionListener(new ActionListener() {
                // 事件处理
                @Override
                public void actionPerformed(ActionEvent e) {
                    Container c = getContentPane();
                    c.setBackground(Color.BLUE);
                }
            });
    
            // 并添加事件监听器
            btDialog.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JDialog dialog = new JDialog();
                    dialog.setBounds(300, 200, 400, 300);
                    dialog.setVisible(true);
                }
            });
    
            add(btBlue);
            add(btDialog);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
    }
    
    /**
     * Java事件监听处理——外部类处理
     *
     */
    class EventListener4 extends JFrame {
        private JButton btBlue, btDialog;
    
        public EventListener4() {
            setTitle("Java GUI 事件监听处理");
            setBounds(100, 100, 500, 350);
            setLayout(new FlowLayout());
            btBlue = new JButton("蓝色");
            btDialog = new JButton("弹窗");
            // 将按钮添加事件监听器
            btBlue.addActionListener(new ColorEventListener(this));
            btDialog.addActionListener(new DialogEventListener());
    
            add(btBlue);
            add(btDialog);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
    }
    
    // 外部类ColorEventListener,实现ActionListener接口
    class ColorEventListener implements ActionListener {
        private EventListener4 el;
    
        ColorEventListener(EventListener4 el) {
            this.el = el;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            Container c = el.getContentPane();
            c.setBackground(Color.BLUE);
        }
    }
    
    
    // 外部类DialogEventListener,实现ActionListener接口
    class DialogEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog dialog = new JDialog();
            dialog.setBounds(300, 200, 400, 300);
            dialog.setVisible(true);
        }
    }
    
    public class ActionListenerTest {
        public static void main(String args[]) {
            new EventListener4();
        }
    }
  • 相关阅读:
    Android Studio 2.3.1导出jar文件不能生成release解决办法
    AndroidStudio 3.0 生成jar包的方法
    Android Studio如何打jar包
    Android Studio 如何打JAR包(修订版)
    6款程序员必备的开源中文处理工具
    Qt5.8 下链接 Mysql 错误以及解决方法(无论 Mysql 是什么版本的,64 位 Qt 要用 64 位的 Mysql 驱动,32 位的 Qt 要用 32 位的Mysql 驱动)
    Go 语言如果按这样改进,能不能火过 Java?
    基于 CSP 的设计思想和 OOP 设计思想的异同
    DELPHI下多线程编程的几个思维误区(QDAC)
    如何使用表单
  • 原文地址:https://www.cnblogs.com/freinds/p/8973140.html
Copyright © 2011-2022 走看看