WindowEvent

来源:互联网 发布:c语言课程设计小论文 编辑:程序博客网 时间:2024/06/07 07:43
import javax.swing.*;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class TestWindowEvent extends JFrame {
    public TestWindowEvent() {
        addWindowListener(new WindowListener() {

            @Override
            /**
             * Handler for window-activated event
             * Invoked when the window is set to be the user's
             * active window, which means the window (or one of its
             * subcomponents) will receive keyboard events
             */
            public void windowActivated(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window activated");
            }

            @Override
            /**
             * Handler for window-closed event
             * Invoked when a window has been closed as the result
             * of calling dispose on the window
             */
            public void windowClosed(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window Closed");
            }

            @Override
            /**
             * Handler for window-closing event
             * Invoked when the user attempts to close the window
             * from the window's system menu. If the program does not
             * explicitly hide or dispose the window while processing
             * this event, the window-closing operation will be cancelled.
             */
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window Closing");
            }

            @Override
            /**
             * Handler for window-deactivated event
             * Invoked when a window is no longer the user's active
             * window, which means that keyboard events will no longer
             * be delivered to the window or its subcomponents.
             */
            public void windowDeactivated(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window deactivated");
            }

            @Override
            /**Handler for window-deiconified event
             * Invoked when a window is changed from a minimized
             * to a normal state.
             */
            public void windowDeiconified(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window deiconified");
            }

            @Override
            /**
             * Handler for window-iconified event
             * Invoked when a window is changed from a normal to a
             * minimized state. For many platforms, a minimized window
             * is displayed as the icon specified in the window's
             * iconImage property
             */
            public void windowIconified(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window Iconified");
            }

            @Override
            /**
             * Handler for window-opened event
             * Invoked the first time a window is made visible.
             */
            public void windowOpened(WindowEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Window opened");
            }
            
        });
    }
    
    public static void main(String[] args) {
        TestWindowEvent frame = new TestWindowEvent();
        frame.setTitle("Test window event");
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

0 0
原创粉丝点击