其他分享
首页 > 其他分享> > 窗口监听2

窗口监听2

作者:互联网

窗口监听2

窗口监听中介绍了给窗口增加点击叉号即关闭的功能,这次给窗口增加一些其他的功能,代码如下:

package com.cxf.gui.six;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestForFrameListen {
    public static void main(String[] args) {
        new MyFrame().addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("x clicked");
            }

            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("back to window");
            }
        });
    }
}

class MyFrame extends Frame{
    public MyFrame(){
        setVisible(true);
        setBounds(200,200,400,400);
    }
}

输出结果:

back to window
back to window
x clicked
x clicked
x clicked
x clicked
back to window
back to window
back to window

点击到别的界面,再点击回窗口时,显示back to window;点击叉号时,显示 x clicked。

这是因为点击回窗口这一事件使windowActivated方法被调用。点击叉号这一事件使windowClosing被调用。

标签:点击,clicked,back,监听,window,窗口,public
来源: https://www.cnblogs.com/cxf-tech/p/15322910.html