AWT WindowAdapter类

1 什么是Java AWT WindowAdapter

WindowAdapter类是一个用于接收窗口事件的抽象(适配器)类。这个类的所有方法都是空的。此类是用于创建侦听器对象的便利类。

2 Java AWT WindowAdapter的语法

public abstract class WindowAdapter
   extends Object
      implements WindowListener, WindowStateListener, WindowFocusListener

3 Java AWT WindowAdapter的构造方法

构造方法 描述
WindowAdapter() 构造一个新的WindowAdapter。

4 Java AWT WindowAdapter的方法

方法 描述
void windowActivated(WindowEvent e) 当窗口被激活时调用。
void windowClosed(WindowEvent e) 当窗口关闭时调用。
void windowClosing(WindowEvent e) 当窗口处于关闭过程中时调用。
void windowDeactivated(WindowEvent e) 当窗口被取消激活时调用。
void windowDeiconified(WindowEvent e) 在取消图标化窗口时调用。
void windowGainedFocus(WindowEvent e) 当 Window 设置为焦点窗口时调用,这意味着 Window 或其子组件之一将接收键盘事件。
void windowIconified(WindowEvent e) 当窗口被图标化时调用。
void windowLostFocus(WindowEvent e) 当 Window 不再是焦点窗口时调用,这意味着键盘事件将不再传递给 Window 或其任何子组件。
void windowOpened(WindowEvent e) 在打开窗口时调用。
void windowStateChanged(WindowEvent e) 当窗口状态改变时调用。

5 Java AWT WindowAdapter的例子

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

package com.yiidian;

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

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

   public AwtAdapterDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtAdapterDemo  awtAdapterDemo = new AwtAdapterDemo();        
      awtAdapterDemo.showWindowAdapterDemo();
   }

   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 showWindowAdapterDemo(){
      headerLabel.setText("Listener in action: WindowAdapter");      

      Button okButton = new Button("OK");

      final Frame aboutFrame = new Frame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowAdapter Demo");
      aboutFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
               aboutFrame.dispose();
         }        
      });    
      Label msgLabel = new Label("Welcome to yiidian.com.");
      msgLabel.setAlignment(Label.CENTER);
      msgLabel.setSize(100,100);
      aboutFrame.add(msgLabel);
      aboutFrame.setVisible(true);
   }
}

输出结果为:

热门文章

优秀文章