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 程序,例如在 D:/ > SWING > com > tutorialspoint > gui >

SwingAdapterDemo.java

package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

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

   public SwingAdapterDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingAdapterDemo  swingAdapterDemo = new SwingAdapterDemo();        
      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 Demo");
      
      aboutFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            aboutFrame.dispose();
         }        
      });    
      JLabel msglabel 
         = new JLabel("Welcome to TutorialsPoint SWING Tutorial.",JLabel.CENTER);
	   aboutFrame.add(msgLabel);
      aboutFrame.setVisible(true);
   }
}

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

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

如果未发生错误,则表示编译成功。使用以下命令运行程序。

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

验证以下输出。

SWING WindowAdapter
swing_event_adapters.htm
广告