Java AWT 图形界面编程事件处理机制 ② ( Frame 窗口事件监听器 WindowListener | 代码示例 )
Posted 韩曙亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java AWT 图形界面编程事件处理机制 ② ( 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 函数 , 此时直接退出了程序 ;
以上是关于Java AWT 图形界面编程事件处理机制 ② ( Frame 窗口事件监听器 WindowListener | 代码示例 )的主要内容,如果未能解决你的问题,请参考以下文章
Java AWT 图形界面编程事件处理机制 ① ( 事件处理步骤 | 创建事件源对象 -> 自定义事件监听器 -> 创建监听器实例对象 -> 事件源绑定事件监听器 )
Java AWT 图形界面编程事件处理机制 ① ( 事件处理步骤 | 创建事件源对象 -> 自定义事件监听器 -> 创建监听器实例对象 -> 事件源绑定事件监听器 )
Java AWT 图形界面编程事件处理机制 ③ ( AWT 中常见的事件和事件监听器 | 低级事件 | 组件事件 | 窗口事件 | 鼠标事件 | 高级事件 | 动作事件 | 事件监听器 )
Java AWT 图形界面编程Container 容器 ② ( Frame 窗口示例 | Panel 示例 | 窗口中文乱码处理 )