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文件内容写入数据库 你可能也喜欢 Java设置以及获取JavaBean私有属性进阶 javascript 大文件下载,分片下载,断点续传 小知识——java实现向目标服务器写入文件 java构造者模式_Java设计模式-Builder构造者模式 Java深入了解DTO及如何使用DTO JavaScript逻辑训练题(一) Java 的get、 set 方法注解 java串口发送十六进制数,本文实例为大家分享了Java实现串口通信的具体代码,供大家参考,具体内容如下1.介绍使用Java实现的串口通信程序,支持十六进制数据的发送与接收。 源码:… java中的数据结构——Map 树莓派 java开发环境_java 基础(三) 搭建Java编译环境(树莓派) Eclipse安装Java类图插件 Maven之idea创建java工程 JavaScript实现倒计时功能 java新特性之自动资源释放 【8】SpringBoot统一接口返回的标准格式R.java 基于SpringBoot+Vue前后端Java快速开发框架 java在指定目录下创建文件夹_怎么用java在指定目录下创建一个文件夹 java四舍五入,推荐BigDecimal 每日一课 | Java 8 Stream List 获取最后一个元素 Java基础编程——回文数