Java基础入门 窗体事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/82082813

大部分GUI应用程序都需要Window窗体对象作为最外层容器,可以说窗体对象是所有GUI应用程序的基础。

在JDK中提供了一个类WindowEvent用于表示窗体事件,在应用程序中当对窗体时间进行处理时,首先需要定义一个类实现WindowListener接口作为窗体监听器,然后通过addWindowListener()方法将窗体对象和窗体监听器绑定。

接下来通过一个案例演示:

import java.awt.*;
import java.awt.event.*;
public class Main{
	public static void main(String[] args)throws Exception{
        Frame f=new Frame("我的窗体");//设置窗体名称
        f.setSize(400, 300);//设置宽和高
        f.setLocation(300, 200);//设置在屏幕中所属位置
        f.setVisible(true);//设置窗体可见
        f.addWindowListener(new WindowListener(){
        	public void windowClosing(WindowEvent e){
        		System.out.println("窗体正在关闭事件");
        		Window window=e.getWindow();
        		window.setVisible(false);
        		window.dispose();
        	}
        	public void windowActivated(WindowEvent e){
        		System.out.println("窗体激活事件");
        	}
        	public void windowClosed(WindowEvent e){
        		System.out.println("窗体关闭事件");
        	}
        	public void windowDeactivated(WindowEvent e){
        		System.out.println("窗体停用事件");
        	}
        	public void windowDeiconified(WindowEvent e){
        		System.out.println("窗体取消图标化事件");
        	}
        	public void windowIconified(WindowEvent e){
        		System.out.println("窗体图标化事件");
        	}
        	public void windowOpened(WindowEvent e){
        		System.out.println("窗体打开事件");
        	}
        });
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/82082813