Java捕獲窗口狀態事件 – JAVA編程語言程序開發技術文章


[java]
package com.han; 
 
import java.awt.Frame; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowStateListener; 
 
import javax.swing.JFrame; 
 
/**
 * 捕獲窗口狀態事件
 * @author HAN
 *
 */ 
public class WindowEvent_2 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = -2809729177140674307L; 
 
    public WindowEvent_2() { 
        // TODO Auto-generated constructor stub  
        addWindowStateListener(new WindowStateListener() { 
 
            @Override 
            public void windowStateChanged(WindowEvent e) { 
                // TODO Auto-generated method stub  
                int oldState = e.getOldState(); 
                int newState = e.getNewState(); 
                String from = null; 
                String to; 
                switch (oldState) { 
                case Frame.NORMAL: 
                    from = "正常化"; 
                    break; 
                case Frame.MAXIMIZED_BOTH: 
                    from = "最大化"; 
                    break; 
                case Frame.ICONIFIED: 
                    from = "圖標化"; 
                    break; 
                }     www.aiwalls.com
                switch (newState) { 
                case Frame.NORMAL: 
                    to = "正常化"; 
                    break; 
                case Frame.MAXIMIZED_BOTH: 
                    to = "最大化"; 
                    break; 
                default: 
                    to = "圖標化"; 
                    break; 
                } 
                System.out.println(from + "—->" + to); 
            } 
             
        }); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  
        WindowEvent_2 frame = new WindowEvent_2(); 
        frame.setTitle("捕獲窗口狀態事件"); 
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setBounds(0, 0, 300, 100); 
    } 
 

作者:Gaowen_HAN

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。