SWING - WindowListener 介面



處理 WindowEvent 的類應實現此介面。該類的物件必須在元件中註冊。可以使用 **addWindowListener()** 方法註冊該物件。

介面宣告

以下是 java.awt.event.WindowListener 介面的宣告 −

public interface WindowListener
   extends EventListener

介面方法

序號 方法和描述
1

void windowActivated(WindowEvent e)

在 Window 被設為活動 Window 時呼叫。

2

void windowClosed(WindowEvent e)

在視窗因在 Window 上呼叫 dispose 而關閉時呼叫。

3

void windowClosing(WindowEvent e)

當用戶嘗試從 Window 的系統選單關閉 Window 時呼叫。

4

void windowDeactivated(WindowEvent e)

在 Window 不再是活動 Window 時呼叫。

5

void windowDeiconified(WindowEvent e)

在 Window 從最小化狀態變為正常狀態時呼叫。

6

void windowIconified(WindowEvent e)

在 Window 從正常狀態變為最小化狀態時呼叫。

7

void windowOpened(WindowEvent e)

在第一次使 Window 可見時呼叫。

繼承的方法

此介面繼承以下介面的方法 −

java.awt.EventListener

WindowListener 示例

在任意編輯器中(例如在 D:/ > SWING > com > tutorialspoint > gui > 中)建立以下 Java 程式。

SwingListenerDemo.java

package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingListenerDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JFrame aboutFrame;
	
   public SwingListenerDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingListenerDemo  swingListenerDemo = new SwingListenerDemo();  
      swingListenerDemo.showWindowListenerDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showWindowListenerDemo(){
      headerLabel.setText("Listener in action: WindowListener");      
      JButton okButton = new JButton("OK");

      aboutFrame = new JFrame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowListener Demo");
      aboutFrame.addWindowListener(new CustomWindowListener());
      
      JPanel panel = new JPanel();      
      panel.setBackground(Color.white);            
      JLabel msglabel = new JLabel("Welcome to TutorialsPoint SWING Tutorial."
         ,JLabel.CENTER);        
      panel.add(msglabel);
      aboutFrame.add(panel);
      aboutFrame.setVisible(true); 
   }
   class CustomWindowListener implements WindowListener {
      public void windowOpened(WindowEvent e) {
      }
      public void windowClosing(WindowEvent e) {
         aboutFrame.dispose();
      }
      public void windowClosed(WindowEvent e) {
      }
      public void windowIconified(WindowEvent e) {
      }
      public void windowDeiconified(WindowEvent e) {
      }
      public void windowActivated(WindowEvent e) {
      }
      public void windowDeactivated(WindowEvent e) {
      }
   }   
}

使用命令提示符編譯程式。轉到 **D:/ > SWING** 並鍵入以下命令。

D:\SWING>javac com\tutorialspoint\gui\SwingListenerDemo.java

如果沒有發生錯誤,則表示編譯成功。使用以下命令執行該程式。

D:\SWING>java com.tutorialspoint.gui.SwingListenerDemo

驗證以下輸出。

SWING WindowListener
swing_event_listeners.htm
廣告