目录

WindowListener

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

接口声明 (Interface Declaration)

以下是java.awt.event.WindowListener接口的声明 -

public interface WindowListener
   extends EventListener

接口方法

Sr.No. 方法和描述
1

void windowActivated(WindowEvent e)

将Window设置为活动Window时调用。

2

void windowClosed(WindowEvent e)

由于在Window上调用dispose而关闭窗口时调用。

3

void windowClosing(WindowEvent e)

当用户尝试从Window的系统菜单关闭Window时调用。

4

void windowDeactivated(WindowEvent e)

当Window不再是活动Window时调用。

5

void windowDeiconified(WindowEvent e)

当Window从最小化状态更改为正常状态时调用。

6

void windowIconified(WindowEvent e)

当Window从正常状态更改为最小化状态时调用。

7

void windowOpened(WindowEvent e)

第一次使窗口可见时调用。

方法继承 (Methods Inherited)

此接口从以下接口继承方法 -

java.awt.EventListener

WindowListener示例

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

SwingListenerDemo.java

package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingListenerDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   public SwingListenerDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingListenerDemo  swingListenerDemo = new SwingListenerDemo();  
      swingListenerDemo.showWindowListenerDemo();
   }
   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 showWindowListenerDemo(){
      headerLabel.setText("Listener in action: WindowListener");      
      JButton okButton = new JButton("OK");
      aboutFrame = new JFrame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowListener Demo");
      aboutFrame.addWindowListener(new CustomWindowListener());
      JPanel panel = new JPanel();      
      panel.setBackground(Color.white);            
      JLabel msglabel = new JLabel("Welcome to IoWiki SWING Tutorial."
         ,JLabel.CENTER);        
      panel.add(msglabel);
      aboutFrame.add(panel);
      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) {
      }
   }   
}

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

D:\SWING>javac com\iowiki\gui\SwingListenerDemo.java

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

D:\SWING>java com.iowiki.gui.SwingListenerDemo

验证以下输出。

SWING WindowListener
↑回到顶部↑
WIKI教程 @2018