JavaSE 窗口事件类(WindowEvent)实现

{相关信息}
窗口事件(WindowEvent)指窗口状态改变的事件,例如当窗口 Window 对象的打开、关闭、激活、停用或者焦点转移到窗口内,
以及焦点移除而生成的事件,一般发生在 Window、Frame、Dialog 等类的对象上。使用窗口事件必须为组件添加一个实现 WindowListener 接口的事件处理器,该接口包含以下 7 种方法:
void windowActivated(WindowEvent e):窗口被激活时发生
void windowClosed(WindowEvent e):窗口关闭之后发生
void windowClosing(WindowEvent e):窗口关闭过程中发生
void windowDeactivated(WindowEvent e):窗口不再处于激活状态时发生
void windowDeiconified(WindowEvent e):窗口大小从最小到正常时发生
void windowIconified(WindowEvent e):窗口从正常到最小时发生
void windowOpened(WindowEvent e):窗口第一次被打开时发生

{相关代码}

package sup.orange.learn;



import java.awt.*;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;



/**

 * Created by re-x on 11/2/14.

 */

public class WindowEventDemo extends Frame {



    String string = null;



    public WindowEventDemo () {

        super();

        init();

    }



    public static void main(String[] args) {

        new WindowEventDemo();

    }



    public void init() {

        addWindowListener(new WindowListener() {

            @Override

            public void windowOpened(WindowEvent e) {

                string = "window opened";

                System.out.println(string);

                repaint();

            }



            @Override

            public void windowClosing(WindowEvent e) {

                string = "window closing";

                System.out.println(string);

                setVisible(false);

                dispose();

                System.exit(0);

            }



            @Override

            public void windowClosed(WindowEvent e) {

                string = "window closed";

                System.out.println(string);

                repaint();

            }



            @Override

            public void windowIconified(WindowEvent e) {

                string = "window iconified";

                System.out.println(string);

                repaint();

            }



            @Override

            public void windowDeiconified(WindowEvent e) {

                string = "window deiconified";

                System.out.println(string);

                repaint();

            }



            @Override

            public void windowActivated(WindowEvent e) {

                string = "window activated";

                System.out.println(string);

                repaint();

            }



            @Override

            public void windowDeactivated(WindowEvent e) {

                string = "window deactivated";

                System.out.println(string);

                repaint();

            }

        });



        setSize(200, 200);

        setVisible(true);



    }



    public void paint(Graphics g) {

        g.drawString(string, 30, 100);

    }

}

 

你可能感兴趣的:(JavaSE)