Java捕獲窗口焦點事件 – JAVA編程語言程序開發技術文章


[java]
package com.han; 
 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowFocusListener; 
 
import javax.swing.JFrame; 
 
/**
 * 捕獲窗口焦點事件
 * @author HAN
 *
 */ 
public class WindowEvent_1 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = 6385933774053272194L; 
 
    public WindowEvent_1() { 
        // TODO Auto-generated constructor stub  
        addWindowFocusListener(new WindowFocusListener() { 
 
            @Override 
            public void windowGainedFocus(WindowEvent e) { 
                // TODO Auto-generated method stub  
                System.out.println("窗口獲得瞭焦點!"); 
            } 
 
            @Override 
            public void windowLostFocus(WindowEvent e) { 
                // TODO Auto-generated method stub  
                System.out.println("窗口失去瞭焦點!"); 
            } 
             
        }); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  
        WindowEvent_1 frame = new WindowEvent_1(); 
        frame.setTitle("捕獲窗口焦點事件"); 
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setBounds(0, 0, 300, 100); 
    } 
 

作者:Gaowen_HAN
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。