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

WindowAdapter 類別 (class) 已經實作 WindowFocusListener 、 WindowListener 、 WindowStateListener 等三個介面 (interface) ,因此可直接改寫 WindowFocusListener 的 windowGainedFocus() 與 windowLostFocus() , WindowListener 的 windowActivated() 、 windowClosed() 、 windowClosing() 、 windowDeactivated() 、 windowDeiconified() 、 windowIconified() 、 windowOpened() , WindowStateListener 的 windowStateChanged() ,不需要逐一都實作。



修飾子
public abstract class WindowAdapter


父類別
java.lang.Object


實作介面
WindowFocusListener
WindowListener
WindowStateListener
EventListener


建構子
public WindowAdapter()


舉例如下
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");
}


編譯後執行,結果如下



中英文術語對照
類別class
介面interface


您可以繼續參考
事件處理


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


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

沒有留言: