Java捕獲窗口焦點事件 -开发者知识库

Java捕獲窗口焦點事件 -开发者知识库,第1张

Java捕獲窗口焦點事件 -开发者知识库,第2张

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

}


最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复