歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java捕獲窗口狀態事件

Java捕獲窗口狀態事件:

  1. package com.han;  
  2.   
  3. import java.awt.Frame;  
  4. import java.awt.event.WindowEvent;  
  5. import java.awt.event.WindowStateListener;  
  6.   
  7. import javax.swing.JFrame;  
  8.   
  9. /** 
  10.  * 捕獲窗口狀態事件 
  11.  * @author HAN 
  12.  * 
  13.  */  
  14. public class WindowEvent_2 extends JFrame {  
  15.   
  16.     /** 
  17.      *  
  18.      */  
  19.     private static final long serialVersionUID = -2809729177140674307L;  
  20.   
  21.     public WindowEvent_2() {  
  22.         // TODO Auto-generated constructor stub   
  23.         addWindowStateListener(new WindowStateListener() {  
  24.   
  25.             @Override  
  26.             public void windowStateChanged(WindowEvent e) {  
  27.                 // TODO Auto-generated method stub   
  28.                 int oldState = e.getOldState();  
  29.                 int newState = e.getNewState();  
  30.                 String from = null;  
  31.                 String to;  
  32.                 switch (oldState) {  
  33.                 case Frame.NORMAL:  
  34.                     from = "正常化";  
  35.                     break;  
  36.                 case Frame.MAXIMIZED_BOTH:  
  37.                     from = "最大化";  
  38.                     break;  
  39.                 case Frame.ICONIFIED:  
  40.                     from = "圖標化";  
  41.                     break;  
  42.                 }  
  43.                 switch (newState) {  
  44.                 case Frame.NORMAL:  
  45.                     to = "正常化";  
  46.                     break;  
  47.                 case Frame.MAXIMIZED_BOTH:  
  48.                     to = "最大化";  
  49.                     break;  
  50.                 default:  
  51.                     to = "圖標化";  
  52.                     break;  
  53.                 }  
  54.                 System.out.println(from + "---->" + to);  
  55.             }  
  56.               
  57.         });  
  58.     }  
  59.   
  60.     /** 
  61.      * @param args 
  62.      */  
  63.     public static void main(String[] args) {  
  64.         // TODO Auto-generated method stub   
  65.         WindowEvent_2 frame = new WindowEvent_2();  
  66.         frame.setTitle("捕獲窗口狀態事件");  
  67.         frame.setVisible(true);  
  68.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  69.         frame.setBounds(00300100);  
  70.     }  
  71.   
  72. }  
Copyright © Linux教程網 All Rights Reserved