Java API 分類導覽 - java.awt.event.WindowListener

WindowListener 介面 (interface) 進行視窗事件的處理,需要實作 windowActivated() 、 windowClosed() 、 windowClosing() 、 windowDeactivated() 、 windowDeiconified() 、 windowIconified() 、 windowOpened() 方法 (method) ,其參數 (parameter) e 為 WindowEvent 物件 (object)



修飾子
public interface WindowListener


父介面
java.util.EventListener


實作方法
名稱敘述
windowActivated()處理視窗成為焦點視窗事件
windowClosed()處理視窗關閉後事件
windowClosing()處理視窗關閉中事件
windowDeactivated()處理視窗失去焦點事件
windowDeiconified()處理視窗取消最小化事件
windowIconified()處理視窗最小化事件
windowOpened()處理開啟視窗事件


舉例如下
import java.awt.*;
import java.awt.event.*;
 
public class Window3Demo extends WindowAdapter {
    Frame frame;
    
    public static void main(String[] args) {
        new Window3Demo();
    }
     
    public Window3Demo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(this);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }
    
    public void windowActivated(WindowEvent e) {
        System.out.println("windowActivated");
    }
    
    public void windowClosed(WindowEvent e) {
        System.out.println("windowClosed");
    }
    
    public void windowClosing(WindowEvent e) {
        System.out.println("windowClosing");
        frame.dispose();
    }
    
    public void windowDeactivated(WindowEvent e) {
        System.out.println("windowDeactivated");
    }
    
    public void windowDeiconified(WindowEvent e) {
        System.out.println("windowDeiconified");
    }
    
    public void windowIconified(WindowEvent e) {
        System.out.println("windowIconified");
    }
    
    public void windowOpened(WindowEvent e) {
        System.out.println("windowOpened");
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:Window3Demo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例的 Window3Demo 繼承自 WindowAdapter 類別,該類別已經實作 WindowListener 介面
public class Window3Demo extends WindowAdapter {


這裡我們直接改寫 WindowListener 中的七個方法,當視窗開啟時,在命令列印出 "windowOpened" ,變成焦點視窗時印出 "windowActivated" ,失去焦點時印出 "windowDeactivated" ,最小化印出 "windowIconified" ,取消最小化印出 "windowDeiconified" ,關閉視窗印出 "windowClosing" ,關閉視窗後印出 "windowClosed"
public void windowActivated(WindowEvent e) {
    System.out.println("windowActivated");
}
    
public void windowClosed(WindowEvent e) {
    System.out.println("windowClosed");
}
    
public void windowClosing(WindowEvent e) {
    System.out.println("windowClosing");
    frame.dispose();
}
    
public void windowDeactivated(WindowEvent e) {
    System.out.println("windowDeactivated");
}
    
public void windowDeiconified(WindowEvent e) {
    System.out.println("windowDeiconified");
}
    
public void windowIconified(WindowEvent e) {
    System.out.println("windowIconified");
}
    
public void windowOpened(WindowEvent e) {
    System.out.println("windowOpened");
}


編譯後執行,結果如下



中英文術語對照
介面interface
方法method
參數parameter
物件object


您可以繼續參考
事件處理


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html

沒有留言: