【Java AWT 图形界面编程】事件处理机制 ② ( Frame 窗口事件监听器 WindowListener | 代码示例 )

news2024/9/22 21:30:01

文章目录

  • 一、Frame 窗口事件监听器 WindowListener
  • 二、Frame 窗口事件监听器 WindowListener 代码示例





一、Frame 窗口事件监听器 WindowListener



在 AWT 中 , 为 Frame 窗口 添加 窗口事件监听器 WindowListener , 可以监听窗口的操作 , 如 :

  • 窗口显示 WindowListener#windowOpened(WindowEvent e)
  • 窗口正在被关闭 WindowListener#windowClosing(WindowEvent e)
  • 窗口完全关闭 WindowListener#windowClosed(WindowEvent e)
  • 窗口最小化 WindowListener#windowIconified(WindowEvent e)
  • 窗口从最小化开始改变 WindowListener#windowDeiconified(WindowEvent e)
  • 窗口获取焦点 WindowListener#windowActivated(WindowEvent e)
  • 窗口失去焦点 WindowListener#windowDeactivated(WindowEvent e)

Frame 窗口事件监听器 WindowListener 原型 : 可以阅读下面的原型中的文档 , 理解窗口的各种监听 ;

/**
 * The listener interface for receiving window events.
 * The class that is interested in processing a window event
 * either implements this interface (and all the methods it
 * contains) or extends the abstract <code>WindowAdapter</code> class
 * (overriding only the methods of interest).
 * The listener object created from that class is then registered with a
 * Window using the window's <code>addWindowListener</code>
 * method. When the window's status changes by virtue of being opened,
 * closed, activated or deactivated, iconified or deiconified,
 * the relevant method in the listener object is invoked, and the
 * <code>WindowEvent</code> is passed to it.
 *
 * @author Carl Quinn
 *
 * @see WindowAdapter
 * @see WindowEvent
 * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: How to Write Window Listeners</a>
 *
 * @since 1.1
 */
public interface WindowListener extends EventListener {
    /**
     * Invoked the first time a window is made visible.
     */
    public void windowOpened(WindowEvent e);

    /**
     * Invoked when the user attempts to close the window
     * from the window's system menu.
     */
    public void windowClosing(WindowEvent e);

    /**
     * Invoked when a window has been closed as the result
     * of calling dispose on the window.
     */
    public void windowClosed(WindowEvent e);

    /**
     * Invoked when a window is changed from a normal to a
     * minimized state. For many platforms, a minimized window
     * is displayed as the icon specified in the window's
     * iconImage property.
     * @see java.awt.Frame#setIconImage
     */
    public void windowIconified(WindowEvent e);

    /**
     * Invoked when a window is changed from a minimized
     * to a normal state.
     */
    public void windowDeiconified(WindowEvent e);

    /**
     * Invoked when the Window is set to be the active Window. Only a Frame or
     * a Dialog can be the active Window. The native windowing system may
     * denote the active Window or its children with special decorations, such
     * as a highlighted title bar. The active Window is always either the
     * focused Window, or the first Frame or Dialog that is an owner of the
     * focused Window.
     */
    public void windowActivated(WindowEvent e);

    /**
     * Invoked when a Window is no longer the active Window. Only a Frame or a
     * Dialog can be the active Window. The native windowing system may denote
     * the active Window or its children with special decorations, such as a
     * highlighted title bar. The active Window is always either the focused
     * Window, or the first Frame or Dialog that is an owner of the focused
     * Window.
     */
    public void windowDeactivated(WindowEvent e);
}




二、Frame 窗口事件监听器 WindowListener 代码示例



代码示例 :

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class HelloAWT {
    private Frame frame;

    private void init() {
        frame = new Frame("AWT 界面编程");

        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("窗口打开");
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("窗口正在关闭");
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("窗口完全关闭");
            }

            @Override
            public void windowIconified(WindowEvent e) {
                System.out.println("窗口最小化");
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                System.out.println("窗口从最小化开始变化");
            }

            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("窗口获取焦点");
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                System.out.println("窗口失去焦点");
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new HelloAWT().init();
    }
}

执行结果 :

在这里插入图片描述

窗口显示时 , 自动回调 windowActivated 先获取焦点 , 然后回调 windowOpened 函数 说明获取了焦点 ,

点击最小化按钮 , 会自动回调 windowIconified 函数 , 然后回调 windowDeactivated 函数 说明失去了焦点 ,

然后点击底部图标的程序图标 , 将其展示到前台 , 会自动回调 windowDeiconified 函数 , 然后自动回调 windowActivated 函数 , 说明获取了焦点 ;

最后点击 关闭按钮 , 会回调 windowClosing 函数 , 此时直接退出了程序 ;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/188359.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

leetcode 1626. Best Team With No Conflicts(最佳无冲突团队)

scores数组中是每个队员的得分&#xff0c;ages数组中为对应队员的年龄&#xff0c; 现在要从这个队里挑选出一些队员&#xff0c;使总得分最高&#xff0c; 挑选时年龄大的要比年龄小的score更高&#xff08;严格大于&#xff09;&#xff0c;才不会产生冲突。 返回最高的得分…

【nestjs+VueJs全栈】- 后端搭建和数据库抽离

先补充一些nestjs的前置知识 控制器 控制器负责处理传入的请求和向客户端返回响应。 控制器的目的是接收应用的特定请求。路由机制控制哪个控制器接收哪些请求。通常&#xff0c;每个控制器有多个路由&#xff0c;不同的路由可以执行不同的操作。 为了创建一个基本的控制器…

React 组件基础

文章目录1. React 组件的两种创建方式1 使用函数创建组件2 使用类创建组件3 抽离为独立 JS 文件2. React 事件处理1.事件绑定2 事件对象3. 有状态组件和无状态组件4. 组件中的 state 和 setState1 state的基本使用2 setState()修改状态从 JSX 中抽离事件处理程序5.事件绑定 thi…

Python初学如何逆袭高手?22个万能公式汇总大全

嗨害大家好鸭&#xff01;我是小熊猫~ 本篇文章共22个万能公式~ 初学者友好~ 源码资料电子书:点击此处跳转文末名片获取 1、一次性进行多个数值的输入 对于数值的输入问题&#xff0c; 是很多笔试题目中经常遇到的问题&#xff0c; 一次性输入多个参数值 &#xff0c; 可以节…

AppiumDesktop学习总结

Appium学习总结 文章目录Appium学习总结一、简介二、环境准备1.下载并配置安卓SDK环境变量2.下载及安装AppiumDesktop3.安装AppiumDesktop4. 启动AppiumDesktop5.安装Python3.x环境6.安装Appium的Python客户端7.安装安卓模拟器8.安装被测APP&#xff1a;9.连接安卓设备三、运行…

NCRE二级 《MS Office高级应用》备考之路

文章目录一、WORD一、易考点二、解题思路二、EXCEL一、易考点二、解题思路三、PPT一、易考点二、解题思路四、其他一、WORD 一、易考点 1.设置页边距、纸张方向、纸张大小、装订线位置&#xff0c;分栏。 2.设置主题、页面边框、添加水印。 3.设置段间距、行间距、特殊格式&…

MySQL从入门到精通(第一篇):MySQL的基本语法及其设计,结合多篇文章

MySQL目录一、数据库入门1. 数据管理技术的三个阶段2. 关系型数据库与非关系型数据库3. 四大非关系型数据库a. 基于列的数据库&#xff08;column-oriented&#xff09;b. 键值对存储&#xff08;Key-Value Stores&#xff09;c. 文档存储&#xff08;Document Stores&#xff…

【日志首次上报积分最多】

题目描述 【日志首次上报最多积分】 日志采集是运维系统的的核心组件。日志是按行生成&#xff0c;每行记做一条&#xff0c;由采集系统分批上报。 如果上报太频繁&#xff0c;会对服务端造成压力;如果上报太晚&#xff0c;会降低用户的体验&#xff1b; 如果一次上报的条数…

【flyway入门及使用】解决生产环境sql更新遗漏

flyway入门及使用 一、简单介绍 flyway开源的数据库版本管理工具 二、为什么要使用flyway 1.自己写的sql没有在全部环境执行 2.别人写的sql没有在全部环境执行 3.有人修改了已经执行过的SQL&#xff0c;期望再次执行 4.需要新增环境做数据迁移 三、flyway是如何工作 1…

雷达流量计的安装方法与应用方向介绍

1、设备介绍 雷达流量计是一种采用微波技术的水位流速探测仪器&#xff0c;结合了成熟的雷达水位计和雷达流速仪的测量技术&#xff0c;主要应用于江河、水库闸口、地下水道管网、灌溉渠道等明渠水位流速测量。该产品可有效地监控水位流速流量变化状态&#xff0c;为监测单位提…

【ThreeJs 初学习】随机三角形的实现方案

随机三角形的实现方案 根据官网的文档整理出一份API文档, 地址是&#xff1a;ThreeJs 官网文档&#xff0c;其目的还是为了方便查阅 下列代码源码地址 上述的截图 就是大致的实现效果。 实现内容 首先我们需要对法向量 以及如何完成一个面有一定的了解&#xff0c;具体了解的内…

Opencv-DNN模块之官方指导:利用DNN模块实现深度学习应用:分类、分割、检测、跟踪等

本文根据 Deep Learning with OpenCV DNN Module: A Definitive Guide 中相关内容进行翻译整理而得&#xff0c;用于今后的学习和工程。 00 前  言 ---   机器视觉研究领域从上个世纪六十年后期就已创立。图像分类和物体检测是计算机视觉领域中的一些最古老的的问题&#x…

CSS - 实现Loading加载动画

Loading加载动画 用CSS都用实现一个loading的加载动画 通过控制 item-loader-container 来实现显示及隐藏 <div class"item-loader-container" id"item-loader-container"><div class"la-ball-running-dots la-2x"><div></…

TVS二极管6.6SMDJ58A/6.6SMDJ58CA参数,有什么区别?

提及6600W高功率TVS二极管&#xff0c;电子工程师们更多想到的可能是DO-218AB封装SM8S系列汽车级瞬态抑制TVS二极管&#xff0c;关于SM8S系列TVS管这方面的知识&#xff0c;之前科普过好多次了。接下来&#xff0c;TVS保护管厂家东沃电子要科普的是另一款6600W的二极管6.6SMDJ系…

最优化问题

一&#xff0c;最优化问题 http://faculty.bicmr.pku.edu.cn/~wenzw/optbook/opt1.pdf 最优化问题&#xff08;也称优化问题&#xff09;泛指定量决策问题&#xff0c;主要关心如何对有限 资源进行有效分配和控制&#xff0c;并达到某种意义上的最优。 最优化问题一般可以描…

算法之美~递归

递归求解问题的分解过程&#xff0c;去的过程叫“递”&#xff0c;回来的过程叫“归”。eg.电影院第几排&#xff1f;f(n) f(n-1) 1 其中,f(1) 1根据递推公式&#xff0c;实现递归代码public int f(int n) {if (n 1) return 1;return f(n-1) 1; }递归需要满足的三个条件一…

2023年谷歌搜索排名规则揭秘,Google排名机制

本文主要分享关于2023年谷歌排名算法的一些机制以及如何操盘才能更好的获取谷歌的自然排名。 本文由光算创作&#xff0c;有可能会被修改和剽窃&#xff0c;我们佛系对待这种行为吧。 要把谷歌排名做起来&#xff0c;你得了解谷歌的排名算法。 Google排名机制是怎么样的&…

调试日志:安卓设备之NFC

adb 查看GPIO 引脚状态 adb shell cd /sys/class/gpio ls cat /sys/kernel/debug/gpio 参考链接 msm8953对应GPIOs 0-141&#xff0c;对应的GPIO Base Addr是从0开始 adb查看GPIOgpio stm8 管脚 配置工具_MSM8953 GPIO口配置说明_zhengjw666的博客-CSDN博客 查看中断 c…

CVE-2022-22972 VMware Workspace ONE Access 身份认证绕过漏洞分析

漏洞描述 5 月 18 日&#xff0c;VMware 发布了一份公告 ( VMSA-2022-0014 )&#xff0c;以解决多个 VMware 产品中的两个漏洞&#xff0c;其中包括CVE-2022-22972&#xff0c;该漏洞在身份认证处理时存在一定缺陷。远程攻击者可通过伪造相关请求信息来绕过身份验证&#xff0…

P2121 拆地毯

# 拆地毯 ## 题目背景 还记得 NOIP 2011 提高组 Day1 中的铺地毯吗&#xff1f;时光飞逝&#xff0c;光阴荏苒&#xff0c;三年过去了。组织者精心准备的颁奖典礼早已结束&#xff0c;留下的则是被人们踩过的地毯。请你来解决类似于铺地毯的另一个问题。 ## 题目描述 ## 输入格…