java中的四種監聽類用法


在此列舉四種方法:

  1. 自身類實現ActionListener接口,作為事件監聽器

  2. 通過匿名類處理

  3. 通過內部類處理

  4. 通過外部類處理

 

下面依次介紹:

 

第一種:自身類實現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;


 

// 構造方法

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.setBackground(Color.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();

}


}

 
 

第二種,通過匿名類處理。

這是比較好的一種方法,我是在2011年開始使用這種方法的。

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;


 

// 構造方法

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.setBackground(Color.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();

}


}

 

第三種:通過內部類處理。

該種方法更符合面向對象編程的思想。

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;


 

// 構造方法

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.setBackground(Color.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();

}


}

 

第四種:通過外部類處理

這種我個人不常用。

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;


 

// 構造方法

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.setBackground(Color.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);

}


}

    你可能注意到為什么我寫了兩個監聽事件,就是加以區分這些方法的區別:

 

    第一種的監聽處理部分,如果有多個(我就寫過三十多個的事件監聽,包含菜單欄按鈕事件監聽和工具欄按鈕事件監聽),那就需要一個個去判斷,從理論上說是影響程序速度的。

   

    第二種和第三種比較常用,如果程序的監聽事件比較少,可以用第二種,匿名類很合適。

   

    第三種符合面向對象編程(可以設置內部類只提供自身類使用,而且方便使用自身類的資源),尤其是適合多個監聽事件的處理,當然也適合第二種方法情況。

   

    第四種是外部類,如果多個監聽事件相同,就可以選用此種方法。

 

    個人愚見:建議初學者掌握這四種方法,側重於第二、三種。怎么學不重要,重要的是達到目的,使自己的GUI編程運用自如。多編程,多思考,提升編程思想;多看別人的代碼,取其精華,有很大幫助!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM