- AWT 教程
- AWT - 首页
- AWT - 概述
- AWT - 环境
- AWT - 控件
- AWT - 事件处理
- AWT - 事件类
- AWT - 事件监听器
- AWT - 事件适配器
- AWT - 布局
- AWT - 容器
- AWT - 菜单
- AWT - 图形
- AWT 有用资源
- AWT - 快速指南
- AWT - 有用资源
- AWT - 讨论
AWT WindowListener 接口
处理 WindowEvent 的类应该实现此接口。该类的对象必须注册到一个组件上。可以使用 addWindowListener() 方法注册对象。
接口声明
以下是java.awt.event.WindowListener接口的声明
public interface WindowListener extends EventListener
接口方法
| 序号 | 方法及描述 |
|---|---|
| 1 | void windowActivated(WindowEvent e) 当窗口被设置为活动窗口时调用。 |
| 2 | void windowClosed(WindowEvent e) 当窗口因调用窗口上的 dispose 而关闭时调用。 |
| 3 | void windowClosing(WindowEvent e) 当用户尝试从窗口的系统菜单中关闭窗口时调用。 |
| 4 | void windowDeactivated(WindowEvent e) 当窗口不再是活动窗口时调用。 |
| 5 | void windowDeiconified(WindowEvent e) 当窗口从最小化状态更改为正常状态时调用。 |
| 6 | void windowIconified(WindowEvent e) 当窗口从正常状态更改为最小化状态时调用。 |
| 7 | void windowOpened(WindowEvent e) 窗口第一次可见时调用。 |
继承的方法
此接口继承自以下接口
java.awt.EventListener
WindowListener 示例
使用您选择的任何编辑器创建以下 Java 程序,例如在D:/ > AWT > com > tutorialspoint > gui >
AwtListenerDemo.javapackage com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public AwtListenerDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
awtListenerDemo.showWindowListenerDemo();
}
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 showWindowListenerDemo(){
headerLabel.setText("Listener in action: WindowListener");
Button okButton = new Button("OK");
aboutFrame = new Frame();
aboutFrame.setSize(300,200);;
aboutFrame.setTitle("WindowListener Demo");
aboutFrame.addWindowListener(new CustomWindowListener());
Label msgLabel = new Label("Welcome to tutorialspoint.");
msgLabel.setAlignment(Label.CENTER);
msgLabel.setSize(100,100);
aboutFrame.add(msgLabel);
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:/ > AWT并键入以下命令。
D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java
如果没有任何错误,则表示编译成功。使用以下命令运行程序。
D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo
验证以下输出
awt_event_listeners.htm
广告