当前位置: 首页 > 文档资料 > JAVA AWT 中文文档 >

WindowAdapter

优质
小牛编辑
120浏览
2023-12-01

介绍 (Introduction)

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

类声明

以下是java.awt.event.WindowAdapter类的声明:

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

类构造函数

SN构造函数和描述
1

WindowAdapter()

类方法

SN方法和描述
1

void windowActivated(WindowEvent e)

激活窗口时调用。

2

void windowClosed(WindowEvent e)

窗口关闭时调用。

3

void windowClosing(WindowEvent e)

窗口处于关闭状态时调用。

4

void windowDeactivated(WindowEvent e)

窗口取消激活时调用。

5

void windowDeiconified(WindowEvent e)

窗口取消图标化时调用。

6

void windowGainedFocus(WindowEvent e)

当Window设置为焦点窗口时调用,这意味着Window或其子组件之一将接收键盘事件。

7

void windowIconified(WindowEvent e)

窗口图标化时调用。

8

void windowLostFocus(WindowEvent e)

当Window不再是焦点窗口时调用,这意味着键盘事件将不再传递给Window或其任何子组件。

9

void windowOpened(WindowEvent e)

打开窗口时调用。

10

void windowStateChanged(WindowEvent e)

更改窗口状态时调用。

方法继承

该类继承以下类中的方法:

  • java.lang.Object

WindowAdapter示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AwtAdapterDemo.java
package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
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 iowiki.");
      msgLabel.setAlignment(Label.CENTER);
      msgLabel.setSize(100,100);
      aboutFrame.add(msgLabel);
      aboutFrame.setVisible(true);
   }
}

使用命令提示符编译程序。 转到D:/ 》 AWT并键入以下命令。

D:\AWT>javac com\iowiki\gui\AwtAdapterDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

D:\AWT>java com.iowiki.gui.AwtAdapterDemo

验证以下输出

AWT WindowAdapter