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);
	}

}


posted on 2012-07-12 05:22  java课程设计例子  阅读(280)  评论(0编辑  收藏  举报