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文件内容写入数据库 你可能也喜欢 Linux系统中JAVA创建文件后权限不足应该如何解决 java调用接口返回form表单_httpClient的post方法使用form格式数据调用接口 Oracle数据库中 DECRYPT_DES 加密解密对应JAVA类 Java实现Zip压缩与解压(解决中文乱码问题) 01背包模板——Java实现 [java] 求两个正整数的最大公约数 javascript学习笔记,三、数据类型-布尔型(Boolean)、未定义型(Undefined)、Null型(空值) java调用ltp_LTP随笔——本地调用ltp之ltp4j java三元运算符 高德地图计算两点间距离(java) java session统计在线人数 北京java培训班,还不了解微服务的同学你们小心了 JAVA DecimalFormat 保留小数位以及四舍五入的陷阱 Java代码动态编译,动态注入功能的实际应用 JAVA导出shape文件&zip java8用stream()流排序和TreeNode权重排序 Java流程控制语句:顺序结构、选择结构(if语句、switch语句) java 计算日出日落时间 Java将多个文件打成一个压缩包 java使用POI操作XWPFDocument中的XWPFRun(文本)对象的属性详解