•     JDK :OpenJDK-11
  •      OS :CentOS 7.6.1810
  •      IDE :Eclipse 2019‑03
  • typesetting :Markdown

code

  1. package per.jizuiku.gui;
  2. import java.awt.Frame;
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5. /**
  6. * @author 给最苦
  7. * @date 2019/06/30
  8. * @blog www.cnblogs.com/jizuiku
  9. */
  10. public class Demo {
  11. /**
  12. * @param args
  13. */
  14. public static void main(String[] args) {
  15. // 创建窗体对象并给出标题
  16. String title = "点击红叉关闭退出程序";
  17. Frame f = new Frame(title);
  18. // 这里是重点,WindowAdapter适配器类
  19. /*
  20. * 事件源 f
  21. * 事件 WindowsListener
  22. * 事件处理 new WindowAdapter(){}
  23. * 事件监听 f.addWindowListener(new WindowAdapter(){})
  24. */
  25. f.addWindowListener(new WindowAdapter() {
  26. // 只要重写 点击红叉 的事件处理函数就好
  27. @Override
  28. public void windowClosing(WindowEvent e) {
  29. // TODO Auto-generated method stub
  30. System.out.println("程序运行结束");
  31. System.exit(0);
  32. }
  33. });
  34. // 可见
  35. f.setVisible(true);
  36. }
  37. }

result

console

  1. 程序运行结束

sourceCode

  1. /*
  2. * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package java.awt.event;
  26. /**
  27. * An abstract adapter class for receiving window events.
  28. * The methods in this class are empty. This class exists as
  29. * convenience for creating listener objects.
  30. * <P>
  31. * Extend this class to create a {@code WindowEvent} listener
  32. * and override the methods for the events of interest. (If you implement the
  33. * {@code WindowListener} interface, you have to define all of
  34. * the methods in it. This abstract class defines null methods for them
  35. * all, so you can only have to define methods for events you care about.)
  36. * <P>
  37. * Create a listener object using the extended class and then register it with
  38. * a Window using the window's {@code addWindowListener}
  39. * method. When the window's status changes by virtue of being opened,
  40. * closed, activated or deactivated, iconified or deiconified,
  41. * the relevant method in the listener
  42. * object is invoked, and the {@code WindowEvent} is passed to it.
  43. *
  44. * @see WindowEvent
  45. * @see WindowListener
  46. * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: Writing a Window Listener</a>
  47. *
  48. * @author Carl Quinn
  49. * @author Amy Fowler
  50. * @author David Mendenhall
  51. * @since 1.1
  52. */
  53. public abstract class WindowAdapter
  54. implements WindowListener, WindowStateListener, WindowFocusListener
  55. {
  56. /**
  57. * Invoked when a window has been opened.
  58. */
  59. public void windowOpened(WindowEvent e) {}
  60. /**
  61. * Invoked when a window is in the process of being closed.
  62. * The close operation can be overridden at this point.
  63. */
  64. public void windowClosing(WindowEvent e) {}
  65. /**
  66. * Invoked when a window has been closed.
  67. */
  68. public void windowClosed(WindowEvent e) {}
  69. /**
  70. * Invoked when a window is iconified.
  71. */
  72. public void windowIconified(WindowEvent e) {}
  73. /**
  74. * Invoked when a window is de-iconified.
  75. */
  76. public void windowDeiconified(WindowEvent e) {}
  77. /**
  78. * Invoked when a window is activated.
  79. */
  80. public void windowActivated(WindowEvent e) {}
  81. /**
  82. * Invoked when a window is de-activated.
  83. */
  84. public void windowDeactivated(WindowEvent e) {}
  85. /**
  86. * Invoked when a window state is changed.
  87. * @since 1.4
  88. */
  89. public void windowStateChanged(WindowEvent e) {}
  90. /**
  91. * Invoked when the Window is set to be the focused Window, which means
  92. * that the Window, or one of its subcomponents, will receive keyboard
  93. * events.
  94. *
  95. * @since 1.4
  96. */
  97. public void windowGainedFocus(WindowEvent e) {}
  98. /**
  99. * Invoked when the Window is no longer the focused Window, which means
  100. * that keyboard events will no longer be delivered to the Window or any of
  101. * its subcomponents.
  102. *
  103. * @since 1.4
  104. */
  105. public void windowLostFocus(WindowEvent e) {}
  106. }

resource

  • [ JDK ] openjdk.java.net
  • [ doc - 参考 ] docs.oracle.com/en/java/javase/11
  • [ 规范 - 推荐 ] yq.aliyun.com/articles/69327
  • [ 规范 - 推荐 ] google.github.io/styleguide
  • [ 源码 ] hg.openjdk.java.net
  • [ OS ] www.centos.org
  • [ IDE ] www.eclipse.org/downloads/packages
  • [ 平台 ] www.cnblogs.com


感谢帮助过 给最苦 的人们。

Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。

规范的命名和代码格式等,有助于沟通和理解。

JVM的配置、监控与优化,比较实用,值得学习。

Java基础 awt Frame 点击叉后,在控制台输出提示信息并关闭程序的更多相关文章

  1. Java基础 awt Button 点击按钮后在控制台输出文字

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  2. Java基础 awt Frame 设置窗体的背景颜色

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  3. Java基础 awt Frame 窗体的大小不可调

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  4. Java基础 awt Frame 设置窗体的大小 位置 可见性

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  5. Java基础 awt Frame 窗体在屏幕的中间显示

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  6. Java基础---AWT

    流式布局FlowLayout package net.zyz; import java.awt.Button; import java.awt.FlowLayout; import java.awt. ...

  7. Java基础 awt Button 鼠标放在按钮上背景颜色改变,鼠标离开背景颜色恢复

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  8. Java基础 awt Graphics2D 生成矩形图片并向内写入字符串

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  9. Java基础 awt Graphics2D 生成矩形图片并向其中画一条直线

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

随机推荐

  1. fastjson 将json字符串转化成List<Map<String, Object>>

    亲测可行,如下: JSON.parseObject(jsonstr, new TypeReference<List<Map<String, Object>>>() ...

  2. linux系统IO操作

    本文重点说明下面内容: 什么是标准IO,什么是文件IO? 什么是Direct IO? O_SYNC标识有什么意义? 各个层面的缓存如何同步? 还在page cache中的脏页可以读写吗? IO路径上的 ...

  3. Centos7 增量备份数据脚本

    #!bin/bash#Automatic Backup Linux System Files#By Author www.jfedu.net#Define VariablesSOURCE_DIR=( ...

  4. P1092 虫食算[搜索]

    这个式子是是由\(A\sim A+N\)组成的,那么\(A\sim A+N\)就只能等于\(0\sim N-1\),因此我们每次对\(A\sim A+N\)的取值做一个新的排列,然后judge一下当前 ...

  5. 实验十四+杜娣+团队项目评审&课程学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/p/11093584.html 这个作业的要求在哪里 https://www.cnblogs.c ...

  6. 20180418模拟赛T1——Seq

    Seq (seq.cpp/c/pas) 题目描述 Description 木吉要去征讨VAN様,所以他现在需要从他身边的人中选出若干位陪同.现在有\(n\)个人站成一行,木吉要从其中选出\(2\)批在 ...

  7. 19、Python标准库: 日期和时间

    一.time时间模块 import time 1 .时间戳   时间戳(timestamp):时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. time_stamp = tim ...

  8. C#中集合ArrayList与Hashtable的使用

    C#中集合ArrayList与Hashtable的使用 http://blog.csdn.net/linukey/article/details/42506819 ArrayList: 一. 注意事项 ...

  9. HTTP1.0、HTTP1.1、HTTP2.0的关系和区别

    一.汇总对比 HTTP1.0 无状态.无连接HTTP1.1 持久连接请求管道化增加缓存处理(新的字段如cache-control)增加Host字段.支持断点传输等(把文件分成几部分)HTTP2.0 二 ...

  10. MongoDB Shell db.runCommand

    db.runCommand()示例 我们使用MongoDB Shell登录到mongos,添加Shard节点 [root@100 shard]# /usr/local/mongoDB/bin/mong ...