Java捕获窗口焦点事件

来源:互联网 发布:遗传算法交叉变异概率 编辑:程序博客网 时间:2024/05/17 07:54

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 stubaddWindowFocusListener(new WindowFocusListener() {@Overridepublic void windowGainedFocus(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("窗口获得了焦点!");}@Overridepublic void windowLostFocus(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("窗口失去了焦点!");}});}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubWindowEvent_1 frame = new WindowEvent_1();frame.setTitle("捕获窗口焦点事件");frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(0, 0, 300, 100);}}


原创粉丝点击