Java捕获窗口焦点事件


Java捕获窗口焦点事件

  1. package com.han;  
  2.   
  3. import java.awt.event.WindowEvent;  
  4. import java.awt.event.WindowFocusListener;  
  5.   
  6. import javax.swing.JFrame;  
  7.   
  8. /** 
  9.  * 捕获窗口焦点事件 
  10.  * @author HAN 
  11.  * 
  12.  */  
  13. public class WindowEvent_1 extends JFrame {  
  14.   
  15.     /** 
  16.      *  
  17.      */  
  18.     private static final long serialVersionUID = 6385933774053272194L;  
  19.   
  20.     public WindowEvent_1() {  
  21.         // TODO Auto-generated constructor stub   
  22.         addWindowFocusListener(new WindowFocusListener() {  
  23.   
  24.             @Override  
  25.             public void windowGainedFocus(WindowEvent e) {  
  26.                 // TODO Auto-generated method stub   
  27.                 System.out.println("窗口获得了焦点!");  
  28.             }  
  29.   
  30.             @Override  
  31.             public void windowLostFocus(WindowEvent e) {  
  32.                 // TODO Auto-generated method stub   
  33.                 System.out.println("窗口失去了焦点!");  
  34.             }  
  35.               
  36.         });  
  37.     }  
  38.   
  39.     /** 
  40.      * @param args 
  41.      */  
  42.     public static void main(String[] args) {  
  43.         // TODO Auto-generated method stub   
  44.         WindowEvent_1 frame = new WindowEvent_1();  
  45.         frame.setTitle("捕获窗口焦点事件");  
  46.         frame.setVisible(true);  
  47.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  48.         frame.setBounds(00300100);  
  49.     }  
  50.   
  51. }  

相关内容