swing窗口事件(WindowLIstener)监听器

一个窗体的所有变化,窗口的打开、关闭等都可以使用这个WindowListener接口进行监听

windowOpened(WindowEvent e)  窗体被打开

windowClosing(WindowEvent e) 窗体关闭

windowIconified(WindowEvent e) 窗体最小化

windowDeiconified(WindowEvent e) 窗体从最小化恢复

windowActivated(WindowEvent e) 窗体被选中

windowDeactivated(WindowEvent e) 取消窗体被选中

演示完整代码如下:

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

public class DemoWindowEvent {

    private JTextArea text = new JTextArea(); 

    public  DemoWindowEvent()

    {

        JFrame frame=new JFrame("WindowListener接口进行监听");

        Dimension dimension=new Dimension();//实例化dimension

        dimension.setSize(400,400);//设置大小

    frame.setSize(dimension); //设置窗体程序的width,和height

    frame.setBackground(Color.white);//设置窗体的背景色

        Point point=new Point(300,200);//设置显示的坐标点    

    frame.setLocation(point);//设置窗体起始位置

        JScrollPane root = new JScrollPane(text);// 给面板增加滚动栏

        frame.add(root);

        frame.addWindowListener(new doWinHandle());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    }

    public static void main(String[] args) { 

        new DemoWindowEvent();

    }    

    public class doWinHandle implements WindowListener {

    @Override

    public void windowOpened(WindowEvent e) {

            text.append("windowOpened--->打开窗口\n");

    }

    @Override

    public void windowClosing(WindowEvent e) {

            text.append("windowClosing--->关闭窗口中...\n");

    }

    @Override

    public void windowClosed(WindowEvent e) {

            text.append("windowClosed--->关闭窗口\n");

    }

    @Override

    public void windowIconified(WindowEvent e) {

            text.append("windowIconified--->最小化窗口\n");

    }

    @Override

    public void windowDeiconified(WindowEvent e) {

            text.append("windowDeiconfied--->恢复窗口\n");

    }

    @Override

    public void windowActivated(WindowEvent e) {

            text.append("windowActivated--->选中窗口\n");

    }

    @Override

    public void windowDeactivated(WindowEvent e) {

            text.append("windowDeactivated--->取消中\n");

    }

}     

}

运行效果

 

来源:

https://blog.csdn.net/xuejiawei123/category_2263953.html