java exit-on-close_JAVA 关闭窗口的设置 DISPOSE_ON_CLOSE和EXIT_ON_CLOSE 的区别 | 学步园

该篇博客介绍了Java Swing中`setDefaultCloseOperation()`方法的使用,用于定义窗口关闭时的行为,如`DISPOSE_ON_CLOSE`将关闭窗口而不退出JVM,而`EXIT_ON_CLOSE`则会在关闭所有窗口后退出程序。示例代码展示了如何在两个窗口上应用这些行为。
摘要由CSDN通过智能技术生成

static int

static int

static int

static int

setDefaultCloseOperation()是用来设定窗口被关闭时候(比如点击了右上角的"x")的行为的。

DISPOSE_ON_CLOSE在窗口被关闭的时候会dispose这个窗口。

EXIT_ON_CLOSE在窗口被关闭的时候会退出JVM。

如果你的程序没有其他线程在运行的话,当所有的窗口都被dispose了之后,JVM也会退出。

package xiya;

import javax.swing.*;

import static javax.swing.JFrame.*;

public class Example10_1 {

public static void main(String[] args) {

JFrame windows1 = new JFrame("窗口一");

JFrame windows2 = new JFrame("窗口二");

windows1.setBounds(200,100,360,260);

windows2.setBounds(560,100,360,260);

windows1.setVisible(true);

windows1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

windows2.setVisible(true);

windows2.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值