Java图形界面事件监听处理之四种方法

我自2009年上大学开始学习Java,由于JavaSE的GUI编程不是很占优势,因而也没有重视过,尤其是事件监听处理。综合看过很多Java讲师的视频,以及网上相关资料,特综合一下,望对大家有帮助,尤其是Java事件编程初学者,愿大家看后不再犹豫用哪种方法而发愁。

大家先看看简单的应用程序截图,考虑一下如何实现。

截图如图一、图二、图三

图一 初始界面截图

图二 蓝色按钮事件处理

图三 弹窗按钮事件处理

在此列举四种方法:

下面依次介绍:

第一种:自身类实现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 codebrother*/// 构造方法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);}// ***************************事件处理***************************@Overridepublic 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();}}

第二种,通过匿名类处理。

这是比较好的一种方法,我是在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;/*** Java事件监听处理——匿名类处理** @author codebrother*/// 构造方法public EventListener2() {// 设置标题栏内容setTitle(“Java GUI 事件监听处理”);// 设置初始化窗口位置setBounds(100, 100, 500, 350);// 设置窗口布局setLayout(new FlowLayout());// 创建按钮对象btBlue = new JButton(“蓝色”);// 添加事件监听器(此处即为匿名类)btBlue.addActionListener(new ActionListener() {// 事件处理@Overridepublic void actionPerformed(ActionEvent e) {// 获得容器,设置容器背景颜色Container c = getContentPane();c.setColor.BLUE);}});// 创建按钮对象,并添加事件监听器btDialog = new JButton(“弹窗”);btDialog.addActionListener(new ActionListener() {// 事件处理@Overridepublic 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;/*** Java事件监听处理——内部类处理** @author codebrother*/// 构造方法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 {@Overridepublic void actionPerformed(ActionEvent e) {Container c = getContentPane();c.setColor.BLUE);}}// 内部类DialogEventListener,实现ActionListener接口class DialogEventListener implements ActionListener {@Overridepublic 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();}}

第四种:通过外部类处理

这种我个人不常用。

看着它洗涤一缕缕阳光,看着它映衬一片片星辉,

Java图形界面事件监听处理之四种方法

相关文章:

你感兴趣的文章:

标签云: