橘子味的心
标题:Swing WindowAdapter类

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

类声明

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

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

类构造函数

编号 构造函数 描述说明
1 WindowAdapter() 无参数构造函数

类方法

编号 类方法 描述说明
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) 当窗口设置为焦点窗口时调用。
7 void windowIconified(WindowEvent e) 在图标化窗口时调用。
8 void windowLostFocus(WindowEvent e) 当窗口不再是焦点窗口时调用。
9 void windowOpened(WindowEvent e) 打开窗口时调用。
10 void windowStateChanged(WindowEvent e) 更改窗口状态时调用。

方法继承

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

  • java.lang.Object

WindowAdapter示例

使用编辑器创建以下Java程序:WindowAdapter.java

package com.yiibai.swing.listener;

import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;

public class WindowAdapterDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public WindowAdapterDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
       WindowAdapterDemo  swingAdapterDemo = new WindowAdapterDemo();        
      swingAdapterDemo.showWindowAdapterDemo();
   }
   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 showWindowAdapterDemo(){
      headerLabel.setText("Listener in action: WindowAdapter");      
      JButton okButton = new JButton("OK");
      final JFrame aboutFrame = new JFrame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowAdapter示例(yiibai.com)");

      aboutFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            aboutFrame.dispose();
         }        
      });    
      JLabel msglabel 
         = new JLabel("欢迎来到易百教程/Swing",JLabel.CENTER);
       aboutFrame.add(msglabel);
      aboutFrame.setVisible(true);
   }
}

执行上面示例代码,得到以下结果:


目录

分类