1. 程式人生 > >Java圖形介面事件監聽處理

Java圖形介面事件監聽處理

                              Java圖形介面事件監聽處理

文章開始把我喜歡的這句話送個大家:這個世界上還有什麼比自己寫的程式碼執行在一億人的電腦上更酷的事情嗎,如果有那就是讓這個數字再擴大十倍。

共四種方法:

1.自身類實現ActionListener介面,作為事件監聽器(適合初學者,事件多時效率低)

2。通過匿名類處理

3.通過內部類處理(更符合面向物件的思想)

4.通過外部類處理(如果有多個重複的監聽事件比較適合)

下面一一介紹

1.自身類實現ActionListener介面

 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;
public class EventListener1 extends JFrame implements ActionListener {
    private JButton btBlue, btDialog;
    /**
     * Java事件監聽處理——自身類實現ActionListener介面,作為事件監聽器
     *
     * @author beyond
     */
    // 構造方法
    public EventListener1() {
        // 設定標題欄內容
        setTitle("Java GUI 事件監聽處理");
        // 設定初始化視窗位置
        setBounds(100, 100, 500, 350);
        // 設定窗口布局
        setLayout(new FlowLayout());
        // 建立按鈕物件
        btBlue = new JButton("藍色");
        // 將按鈕新增事件監聽器
        btBlue.addActionListener(this);
        // 建立按鈕物件
        btDialog = new JButton("彈窗");
        // 將按鈕新增事件監聽器
        btDialog.addActionListener(this);
        // 把按鈕容器新增到JFrame容器上
        add(btBlue);
        add(btDialog);
        // 設定視窗視覺化
        setVisible(true);
        // 設定視窗關閉
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // ***************************事件處理***************************
    @Override
    public void actionPerformed(ActionEvent e) {
        // 判斷最初發生Event事件的物件
        if (e.getSource() == btBlue) {
            // 獲得容器
            Container c = getContentPane();
            // 設定容器背景顏色
            c.setColor.BLUE);
         }
        else if (e.getSource() == btDialog) {
            // 建立JDialog視窗物件
            JDialog dialog = new JDialog();
            dialog.setBounds(300, 200, 400, 300);
            dialog.setVisible(true);
        }
    }
    // ***************************主方法***************************
    public static void main(String[] args) {
        new EventListener1();
    }
}

2.匿名類處理

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;
public class EventListener2 extends JFrame {
    private JButton btBlue, btDialog;
    /**
     * Java事件監聽處理——匿名類處理
     *
     * @author beyond
     */
    // 構造方法
    public EventListener2() {
        // 設定標題欄內容
        setTitle("Java GUI 事件監聽處理");
        // 設定初始化視窗位置
        setBounds(100, 100, 500, 350);
        // 設定窗口布局
        setLayout(new FlowLayout());
        // 建立按鈕物件
        btBlue = new JButton("藍色");
        // 新增事件監聽器(此處即為匿名類)
        btBlue.addActionListener(new ActionListener() {
            // 事件處理
            @Override
            public void actionPerformed(ActionEvent e) {
                // 獲得容器,設定容器背景顏色
                Container c = getContentPane();
                c.setColor.BLUE);
             }
        });
        // 建立按鈕物件,並新增事件監聽器
        btDialog = new JButton("彈窗");
        btDialog.addActionListener(new ActionListener() {
            // 事件處理
            @Override
            public void actionPerformed(ActionEvent e) {
                // 建立JDialog視窗物件
                JDialog dialog = new JDialog();
                dialog.setBounds(300, 200, 400, 300);
                dialog.setVisible(true);
            }
        });
        // 把按鈕容器新增到JFrame容器上
        add(btBlue);
        add(btDialog);
        // 設定視窗視覺化
        setVisible(true);
        // 設定視窗關閉
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // ***************************主方法***************************
    public static void main(String[] args) {
        new EventListener2();
    }

}

3.內部類處理

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;
public class EventListener3 extends JFrame {
    private JButton btBlue, btDialog;
    /**
     * Java事件監聽處理——內部類處理
     *
     * @author beyond
     */
    // 構造方法
    public EventListener3() {
        // 設定標題欄內容
        setTitle("Java GUI 事件監聽處理");
        // 設定初始化視窗位置
        setBounds(100, 100, 500, 350);
        // 設定窗口布局
        setLayout(new FlowLayout());
        // 建立按鈕物件
        btBlue = new JButton("藍色");
        // 新增事件監聽器物件(面向物件思想)
        btBlue.addActionListener(new ColorEventListener());
        btDialog = new JButton("彈窗");
        btDialog.addActionListener(new DialogEventListener());
        // 把按鈕容器新增到JFrame容器上
        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.setColor.BLUE);
         }
    }
    // 內部類DialogEventListener,實現ActionListener介面
    class DialogEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 建立JDialog視窗物件
            JDialog dialog = new JDialog();
            dialog.setBounds(300, 200, 400, 300);
            dialog.setVisible(true);
        }
    }
    // ***************************主方法***************************
    public static void main(String[] args) {
        new EventListener3();
    }

}

4.外部類處理

 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;
public class EventListener4 extends JFrame {
    private JButton btBlue, btDialog;
    /**
     * Java事件監聽處理——外部類處理
     *
     * @author beyond
     */
    // 構造方法
    public EventListener4() {
        // 設定標題欄內容
        setTitle("Java GUI 事件監聽處理");
        // 設定初始化視窗位置
        setBounds(100, 100, 500, 350);
        // 設定窗口布局
        setLayout(new FlowLayout());
        // 建立按鈕物件
        btBlue = new JButton("藍色");
        // 將按鈕新增事件監聽器
        btBlue.addActionListener(new ColorEventListener(this));
        // 建立按鈕物件
        btDialog = new JButton("彈窗");
        // 將按鈕新增事件監聽器
        btDialog.addActionListener(new DialogEventListener());
        // 把按鈕容器新增到JFrame容器上
        add(btBlue);
        add(btDialog);
        // 設定視窗視覺化
        setVisible(true);
        // 設定視窗關閉
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // ***************************主方法***************************
    public static void main(String[] args) {
        new EventListener4();
    }
}
// 外部類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.setColor.BLUE);
     }
}
// 外部類DialogEventListener,實現ActionListener介面
class DialogEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        // 建立JDialog視窗物件
        JDialog dialog = new JDialog();
        dialog.setBounds(300, 200, 400, 300);
        dialog.setVisible(true);
    }
}

加油吧,程式設計師!