总结一下WindowListener的用法
记录一下便于自己查看
1.WindowListener
java.awt.event
接口 WindowListener
public interface WindowListener
extends
EventListener
用于接收窗口事件的侦听器接口。旨在处理窗口事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象类 WindowAdapter(仅重写所需的方法)。然后使用窗口的 addWindowListener 方法将从该类所创建的侦听器对象向该 Window 注册。当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将 WindowEvent 传递给该方法。
windowOpened
void windowOpened(WindowEvent e)
窗口首次变为可见时调用。
windowClosing
void windowClosing(WindowEvent e)
用户试图从窗口的系统菜单中关闭窗口时调用。
windowClosed
void windowClosed(WindowEvent e)
因对窗口调用 dispose 而将其关闭时调用。
windowIconified
void windowIconified(WindowEvent e)
窗口从正常状态变为最小化状态时调用。对于很多平台而言,已最小化的窗口将显示为窗口 iconImage 属性中所指定的图标。
windowDeiconified
void windowDeiconified(WindowEvent e)
窗口从最小化状态变为正常状态时调用。
windowActivated
void windowActivated(WindowEvent e)
将 Window 设置为活动 Window 时调用。只有框架或对话框可以成为活动 Window。本机的窗口系统可能使用特殊装饰表示活动 Window 或其子窗口,如高亮显示标题栏。活动 Window 要么是聚焦 Window,要么是作为聚焦 Window 所有者的第一个 Frame 或 Dialog。
windowDeactivated
void windowDeactivated(WindowEvent e)
当 Window 不再是活动 Window 时调用。只有框架或对话框可以成为活动 Window。本机的窗口操作系统可能使用特殊装饰表示活动 Window 或其子窗口,如高亮显示标题栏。活动 Window 要么是聚焦 Window,要么是作为聚焦 Window 所有者的第一个 Frame 或 Dialog。
1.实现WindowListener接口
1 package tst; 2 3 import java.awt.TextArea; 4 import java.awt.event.WindowEvent; 5 import java.awt.event.WindowListener; 6 7 import javax.swing.JFrame; 8 9 public class Test { 10 final static int x=100; 11 final static int y=50; 12 final static int width=500; 13 final static int height=300; 14 static TextArea text; 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 text=new TextArea(); 18 JFrame f = new JFrame(); 19 f.add(text); 20 f.setTitle("Closing Frame with Window Listener"); 21 f.setBounds(x,y,width,height); 22 f.addWindowListener(new MyWindowListener()); 23 f.setVisible(true); 24 } 25 26 static class MyWindowListener implements WindowListener { 27 28 @Override 29 public void windowOpened(WindowEvent e) { 30 // TODO Auto-generated method stub 31 System.out.println("windowOpened"); 32 text.setText("窗口首次变为可见时调用"); 33 } 34 35 @Override 36 public void windowClosing(WindowEvent e) { 37 // TODO Auto-generated method stub 38 System.out.println("windowClosing"); 39 text.setText("用户试图从窗口的系统菜单中关闭窗口时调用"); 40 } 41 42 @Override 43 public void windowClosed(WindowEvent e) { 44 // TODO Auto-generated method stub 45 System.out.println("windowClosed"); 46 text.setText("因对窗口调用 dispose 而将其关闭时调用"); 47 } 48 49 @Override 50 public void windowIconified(WindowEvent e) { 51 // TODO Auto-generated method stub 52 System.out.println("windowIconified"); 53 text.setText("窗口从正常状态变为最小化状态时调用"); 54 } 55 56 @Override 57 public void windowDeiconified(WindowEvent e) { 58 // TODO Auto-generated method stub 59 System.out.println("windowDeiconified"); 60 text.setText("窗口从最小化状态变为正常状态时调用"); 61 } 62 63 @Override 64 public void windowActivated(WindowEvent e) { 65 // TODO Auto-generated method stub 66 System.out.println("windowActivated"); 67 text.setText("将 Window 设置为活动 Window 时调用"); 68 } 69 70 @Override 71 public void windowDeactivated(WindowEvent e) { 72 // TODO Auto-generated method stub 73 System.out.println("windowDeactivated"); 74 text.setText("当 Window 不再是活动 Window 时调用"); 75 } 76 77 78 79 } 80 81 82 }
2.继承WindowAdapter
java.awt.event
类 WindowAdapter
public abstract class WindowAdapter
extends
Object
implements
WindowListener,
WindowStateListener,
WindowFocusListener
接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。
扩展此类可创建 WindowEvent 侦听器并为所需事件重写该方法。(如果要实现 WindowListener 接口,则必须定义该接口内的所有方法。此抽象类将所有方法都定义为 null,所以只需针对关心的事件定义方法。)
使用扩展的类可以创建侦听器对象,然后使用窗口的 addWindowListener 方法向该窗口注册侦听器。当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将 WindowEvent 传递给该方法。
void windowActivated(WindowEvent e)
激活窗口时调用。
void windowClosed(WindowEvent e)
当窗口已被关闭时调用。
void windowClosing(WindowEvent e)
窗口正处在关闭过程中时调用。
void windowDeactivated(WindowEvent e)
停用窗口时调用。
void windowDeiconified(WindowEvent e)
取消图标化窗口时调用。
void windowGainedFocus(WindowEvent e)
该 Window 被设置为聚焦 Window 时调用,聚焦 Window 意味着该 Window 或其某个子组件将接收键盘事件。
void windowIconified(WindowEvent e)
图标化窗口时调用。
void windowLostFocus(WindowEvent e)
该 Window 不再为聚焦 Window 时调用,不再为聚焦 Window 意味着键盘事件不再传递到该 Window 或其任意子组件。
void windowOpened(WindowEvent e)
已打开窗口时调用。
void windowStateChanged(WindowEvent e)
窗口状态改变时调用。
1 package tst; 2 3 import java.awt.TextArea; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6 7 import javax.swing.JFrame; 8 9 public class Test { 10 final static int x=100; 11 final static int y=50; 12 final static int width=500; 13 final static int height=300; 14 static TextArea text; 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 text=new TextArea(); 18 JFrame f = new JFrame(); 19 f.add(text); 20 f.setTitle("Closing Frame with Window Listener"); 21 f.setBounds(x,y,width,height); 22 f.addWindowListener(new MyWindowListener()); 23 f.setVisible(true); 24 } 25 26 static class MyWindowListener extends WindowAdapter { 27 28 @Override 29 public void windowOpened(WindowEvent e) { 30 // TODO Auto-generated method stub 31 System.out.println("windowOpened"); 32 text.setText("窗口首次变为可见时调用"); 33 } 34 35 @Override 36 public void windowClosing(WindowEvent e) { 37 // TODO Auto-generated method stub 38 System.out.println("windowClosing"); 39 text.setText("用户试图从窗口的系统菜单中关闭窗口时调用"); 40 } 41 42 @Override 43 public void windowClosed(WindowEvent e) { 44 // TODO Auto-generated method stub 45 System.out.println("windowClosed"); 46 text.setText("因对窗口调用 dispose 而将其关闭时调用"); 47 } 48 49 @Override 50 public void windowIconified(WindowEvent e) { 51 // TODO Auto-generated method stub 52 System.out.println("windowIconified"); 53 text.setText("窗口从正常状态变为最小化状态时调用"); 54 } 55 56 @Override 57 public void windowDeiconified(WindowEvent e) { 58 // TODO Auto-generated method stub 59 System.out.println("windowDeiconified"); 60 text.setText("窗口从最小化状态变为正常状态时调用"); 61 } 62 63 @Override 64 public void windowActivated(WindowEvent e) { 65 // TODO Auto-generated method stub 66 System.out.println("windowActivated"); 67 text.setText("将 Window 设置为活动 Window 时调用"); 68 } 69 70 @Override 71 public void windowDeactivated(WindowEvent e) { 72 // TODO Auto-generated method stub 73 System.out.println("windowDeactivated"); 74 text.setText("当 Window 不再是活动 Window 时调用"); 75 } 76 77 78 } 79 80 81 }