歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java捕獲窗口焦點事件

Java捕獲窗口焦點事件

日期:2017/3/1 10:16:05   编辑:Linux編程

Java捕獲窗口焦點事件

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