frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 和 system.exit( )区别

本文详细解析了Java中窗口关闭操作的两种主要方法——`frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)`与`System.exit()`的区别,并阐述了它们在实际开发中的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 和 system.exit( )区别

2011-10-11 11:29 lvxin831123  |  分类:JAVA相关  |  浏览10011次
2011-10-12 19:28 提问者采纳
System.exit(0)是退出整个程序,如果有多个窗口,全部都销毁退出。
frame.setDefaultCloseOperation()是设置用户在此窗体上发起 "close" 时默认执行的操作。必须指定以下选项之一: 

DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理该操作。 
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener 对象后自动隐藏该窗体。 
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener 的对象后自动隐藏并释放该窗体。 
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit 方法退出应用程序。仅在应用程序中使用。 
默认情况下,该值被设置为 HIDE_ON_CLOSE

也就是说没有设置的话,默认点关闭时只是隐藏窗体,在后台进程中还可以看到,如果有多个窗口,只是销毁调用dispose的窗口,其他窗口仍然存在,整个应用程序还是处于运行状态。
追问
谢谢,写的很详细

/** * This demo showcases how to use Alibaba Cloud's DashScope model for recognition from microphone * audio input to text Type 'q' to finish recording and recognition. */ public class RecognizeSpeechFromMicrophoneUsingFlowable { public static void main(String[] args) { // set exit flags boolean[] shouldExit = {false}; Object exitFlag = new Object(); // Start a new thread to record and recognize new Thread( () -> { try { startRecordingAndRecognition(exitFlag, shouldExit); } catch (Exception e) { e.printStackTrace(); System.exit(0); } }) .start(); // Exit the program waitForExitSignal(exitFlag, shouldExit); } // Wait for exit signal by key press. private static void waitForExitSignal(Object exitFlag, boolean[] shouldExit) { // Create a hidden JFrame to capture key events JFrame frame = new JFrame(); frame.setUndecorated(true); frame.setSize(1, 1); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println("Press 'Ctrl+C' to stop recording and recognition..."); frame.addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_C) { synchronized (exitFlag) { shouldExit[0] = true; exitFlag.notifyAll(); // 通知录音线程退出 } System.out.println("Exit signal received. Exiting..."); } } }); frame.setVisible(true); } private static void startRecordingAndRecognition(Object exitFlag, boolean[] shouldExit) throws NoApiKeyException { // Create a Flowable<ByteBuffer> for streaming audio data Flowable<ByteBuffer> audioSource = createAudioSourceWithControl(exitFlag, shouldExit); // Create speech Recognizer Recognition recognizer = new Reco
最新发布
03-26
package com.enterprise; import com.enterprise.controller.AttendanceController; import com.enterprise.controller.EmployeeController; import com.enterprise.controller.EmployeeListController; import com.enterprise.dao.AttendanceDAO; import com.enterprise.dao.EmployeeDAO; import com.enterprise.view.AttendanceView; import com.enterprise.view.EmployeeListView; import com.enterprise.view.EmployeeView; import javax.swing.*; /** * @Author: 羽赫 * @Description: */ public class Main { public static void main(String[] args) { // database connection information String url = "jdbc:mysql://localhost:3307/enterprise"; String user = "root"; String password = "123456"; // create database access objects EmployeeDAO employeeDAO = new EmployeeDAO(url, user, password); AttendanceDAO attendanceDAO = new AttendanceDAO(url, user, password); // create views EmployeeView employeeView = new EmployeeView(); EmployeeListView employeeListView = new EmployeeListView(); AttendanceView attendanceView = new AttendanceView(); // create controllers EmployeeController employeeController = new EmployeeController(employeeView, employeeDAO); EmployeeListController employeeListController = new EmployeeListController(employeeListView, employeeDAO); AttendanceController attendanceController = new AttendanceController(attendanceView, attendanceDAO); // create main frame and add views JFrame frame = new JFrame("Enterprise Attendance System"); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("Add Employee", employeeView); tabbedPane.addTab("Employee List", employeeListView); tabbedPane.addTab("Attendance Records", attendanceView); frame.add(tabbedPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值