Java关闭窗体的六种方法 Post author:xfxia Post published:2023年8月23日 Post category:java 1.使用JFrame的enableEvents和processWindowEvent //Frame1.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Frame1 extends JFrame { public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); this.setSize(new Dimension(400, 300)); this.setTitle(“Frame1”); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } 2.直接实现WindowListener接口 //Frame1.java import java.awt.*; import java.awt.event.*; public class Frame1 extends Frame implements WindowListener { public Frame1() { this.setSize(new Dimension(400, 300)); this.setTitle(“Frame1”); this.addWindowListener(this); } public void windowClosing(WindowEvent windowEvent) { System.exit(0); } public void windowOpened(WindowEvent windowEvent) { } public void windowClosed(WindowEvent windowEvent) { } public void windowIconified(WindowEvent windowEvent) { } public void windowDeiconified(WindowEvent windowEvent) { } public void windowActivated(WindowEvent windowEvent) { } public void windowDeactivated(WindowEvent windowEvent) { } } 3.直接继承窗体适配器WindowAdapter //Frame1.java import java.awt.*; import java.awt.event.*; public class Frame1 extends WindowAdapter { public Frame1() { Frame f=new Frame(); f.setSize(new Dimension(400, 300)); f.setTitle(“Frame1”); f.addWindowListener(this); f.setVisible(true); } public static void main(String[] s){ new Frame1(); } public void windowClosing(WindowEvent windowEvent) { System.exit(0); } } 4.间接继承窗体适配器WindowAdapter //Frame1.java import java.awt.*; import java.awt.event.*; public class Frame1 extends Frame { public Frame1() { this.setSize(new Dimension(400, 300)); this.setTitle(“Frame1”); this.addWindowListener(new winAdapter()); this.setVisible(true); } public static void main(String[] s){ new Frame1(); } } class winAdapter extends WindowAdapter{ public void windowClosing(WindowEvent windowEvent) { System.exit(0); } } 5.间接实现WindowListener接口 //Frame1.java import java.awt.*; import java.awt.event.*; public class Frame1 extends Frame { public Frame1() { this.setSize(new Dimension(400, 300)); this.setTitle(“Frame1”); this.addWindowListener(new winEventHandle()); this.setVisible(true); } public static void main(String[] s){ new Frame1(); } } class winEventHandle implements WindowListener { public void windowClosing(WindowEvent windowEvent) { System.exit(0); } public void windowOpened(WindowEvent windowEvent) { } public void windowClosed(WindowEvent windowEvent) { } public void windowIconified(WindowEvent windowEvent) { } public void windowDeiconified(WindowEvent windowEvent) { } public void windowActivated(WindowEvent windowEvent) { } public void windowDeactivated(WindowEvent windowEvent) { } } 6.使用Inner Class //Frame1.java import java.awt.*; import java.awt.event.*; public class Frame1{ public Frame1(){ Frame f=new Frame(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setSize(new Dimension(400, 300)); f.setVisible(true); } public static void main(String[] s){ new Frame1(); } } Jframe的关闭方法: setDefaultCloseOperation(EXIT_ON_CLOSE); frame的关闭方法如下: this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); Tags: java Read more articles Previous Postjavaweb美食网站在下一篇文章java压缩json串后存入数据库_java/SpringBoot项目将json文件内容写入数据库 你可能也喜欢 【3-4】《Java面向对象高级知识》——继承、重写和重载、final关键字、抽象类、接口、多态、Object类、内部类、包装类、可变参数、递归 全面吃透JAVA Stream流操作,让代码更加的优雅 Java程序设计 作业2(继承与多态) javascript基础学习 最新:深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)周志明 JavaScript文档对象总结 java中把一个文件中的内容复制到另外一个文件原内容的后面_Java 实例 – 将文件内容复制到另一个文件… Java在任意位置拼接字符串 (亲测)java将Object对象转换成实体类对象 Java 数组全解析 Java—遇到报错不要慌,教你如何在打印错误信息的同时让程序继续运行。 java压缩zip文件中文乱码问题 ArcGIS api for javascript 3.x通过“url?token=“动态拼接的方式访问server的安全服务 JavaScript刷LeetCode拿offer-滑动窗口 java常用的API(四):Math类,Random类,Integer类,BigInteger类,BigDecimal类 java的类和对象 JSON对象(JSONObject)与Java对象之间的转换(JSON对象在前端、后端之间的交互转换) JAVA实现拼图游戏 Java实现xml与map互转 java判断手机号是否合法