GUI编程——窗口监听

窗口监听

 

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.BLUE);
        setBounds(100,100,200,200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());

        this.addWindowListener(
            //匿名内部类
            new WindowAdapter(){
                //关闭窗口
                @Override
                public void windowClosing(WindowEvent e){
                    System.out.println("windowClosing");
                    System.exit(0);
                }
                //激活窗口
                @Override
                public void windowActivated(WindowEvent e){
                    System.out.println("windowActivated");
                }
            }
        );
    }

    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e){
            setVisible(false);   //隐藏窗口,通过按钮,隐藏当前窗口
            System.exit(0);  //正常退出
        }
    }
}

 

posted @ 2021-04-12 16:08  cengxuyuan  阅读(52)  评论(0)    收藏  举报