Java图形界面事件监听处理

本文介绍了Java图形界面中四种事件监听处理方法:自身类实现ActionListener接口、匿名类处理、内部类处理及外部类处理,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

加油吧,程序员!

6.2.4 事件适配器 适配器类实现一个对应的所有接口,只是方法为空。 public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} } 表6.1 Listener接口与对应的适配器(Adapter)类 接口名称 适配器名称 ComponentListener ComponentAdapter ContainerListener ContainerAdapter FocusListener FocusAdapter KeyListener KeyAdapter MouseListener MouseAdapter MouseMotionListener MouseMotionAdapter WindowListener WindowAdapter (7)鼠标运动事件 鼠标运动事件发生在鼠标移过某个组件时。 任何组件都可以产生这些事件,通过接口 MouseMotionListener 实现。 该接口有两个方法: mouseDragged(MouseEvent) mouseMove(MouseEvent) MouseMotionListener 没有自己的事件类型,替代它的是 MouseEvent 。 被用在 MouseEvent 对象上的方法它都可以使用: getClickCount( ) 返回鼠标被单击次数(整数) getPoint( ) 返回鼠标被单击位置的 x ,y 坐标 getX( ) 返回 x 位置 getY( ) 返回 y 位置 (8)窗口事件(WindowEvent) 窗口事件发生在用户打开或关闭一个诸如 Frame 或 Window 的窗口时。任何组件都可以产生这些事件,为了支持它们,类必须实现接口:WindowListener。 接口 WindowListener 中有七个方法: windowActivated(WindowEvent) 窗口被激活 windowClosed (WindowEvent) 窗口已关闭 windowClosing (WindowEvent) 窗口正在关闭 windowDeactivated (WindowEvent) 窗口失效 windowDeiconified (WindowEvent) 窗口正常化时 windowIconified (WindowEvent) 窗口最小化时 windowOpened (WindowEvent) 窗口被打开 (5)键盘事件(KeyEvent) 发生在键盘上的某个键被按下时。 类为了能够处理这些事件必须实现接口 KeyListener 。 每个产生一个键盘事件的组件上要调用方法 addKeyListener( ) 在接口 KeyListener 中有三个方法: public void keyPressed(KeyEvent evt) { //…… } public void keyReleased(KeyEvent evt) { //…… } public void keyTyped(KeyEvent evt) { //…… } 在 KeyEvent 对象上可以使用的方法: getKeyChar( ) 返回与事件相关的键盘字符的 Unicode 码 . . . . . . . . . . . . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值