• GUI 窗口监听事件


    编写代码 TestWindowListener测试类

    
    package com.xiang.lesson03;
    
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    //窗口监听
    public class TestWindowListener {
        public static void main(String[] args) {
            new WindowFrame();
        }
    }
    
    class WindowFrame extends Frame {
        public WindowFrame() {
            setVisible(true);
            setBackground(Color.blue);
            setBounds(100, 100, 300, 300);
    //        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) {
                    WindowFrame source = (WindowFrame) e.getSource();
                    source.setTitle("被激活了");
                    System.out.println("windowActivated");
                }
            });
        }
    
        //    监听器
    //    内部类
        class MyWindowListener extends WindowAdapter {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false); //隐藏
                System.exit(0);
            }
        }
    }
    

    运行结果

  • 相关阅读:
    DP 训练题目
    洛谷 P1736 创意吃鱼法
    树形背包
    树形DP
    轻松完爆Helm私有仓库
    轻松完爆Helm公共仓库
    一分钟轻松玩转Helm
    ceph -s 出现 mon is allowing insecure global_id reclaim
    Django下载与简介
    部署ceph集群 (Nautilus版)
  • 原文地址:https://www.cnblogs.com/d534/p/15110098.html
Copyright © 2020-2023  润新知