Java時間監聽的四種實現方式

來源:互聯網
上載者:User

標籤:object   bsp   發送   ==   operation   ati   col   layout   對象   

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();    }}

 

Java時間監聽的四種實現方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.