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

WindowListener 介面 (interface) 的 windowOpened() 方法 (method) 用來設定視窗開啟時的動作。



修飾子與參數
void windowOpened(WindowEvent e)


windowOpened() 沒有回傳值 (return value) ,參數 (paramenter) 為 WindowEvent 型態 (type) 的物件 (object) 。


舉例如下
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
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
事件處理


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


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

沒有留言: