AWT WindowListener类

1 什么是Java AWT WindowListener

处理 WindowEvent 的类应该实现这个接口。该类的对象必须注册到一个组件中。可以使用 addWindowListener() 方法注册该对象。

2 Java AWT WindowListener的语法

public interface WindowListener
   extends EventListener

3 Java AWT WindowListener的方法

方法 描述
void windowActivated(WindowEvent e) 当窗口设置为活动窗口时调用。
void windowClosed(WindowEvent e) 由于在窗口上调用 dispose 导致窗口关闭时调用。
void windowClosing(WindowEvent e) 当用户尝试从窗口的系统菜单关闭窗口时调用。
void windowDeactivated(WindowEvent e) 当窗口不再是活动窗口时调用。
void windowDeiconified(WindowEvent e) 当窗口从最小化状态变为正常状态时调用。
void windowIconified(WindowEvent e) 当窗口从正常状态更改为最小化状态时调用。
void windowOpened(WindowEvent e) 第一次使窗口可见时调用。

4 Java AWT WindowListener的例子

让我们看一个简单的Java AWT WindowListener类示例。

package com.yiidian;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class AwtListenerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   private Frame aboutFrame;

   public AwtListenerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtListenerDemo  awtListenerDemo = new AwtListenerDemo();  
      awtListenerDemo.showWindowListenerDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("一点教程网:Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
   
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      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");      

      Button okButton = new Button("OK");

      aboutFrame = new Frame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowListener Demo");
      aboutFrame.addWindowListener(new CustomWindowListener());

      Label msgLabel = new Label("Welcome to yiidian.com.");
      msgLabel.setAlignment(Label.CENTER);
      msgLabel.setSize(100,100);
      aboutFrame.add(msgLabel);
      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) {
      }
   }   
}

输出结果为:

热门文章

优秀文章