Swing---WindowConstants

Java窗口关闭行为解析
本文介绍了Java Swing中JFrame窗口的关闭行为,并详细解释了WindowConstants接口中的常量及其作用,包括不同关闭选项如DO_NOTHING_ON_CLOSE、HIDE_ON_CLOSE、DISPOSE_ON_CLOSE和EXIT_ON_CLOSE的具体含义。

  Java桌面开发过程中,很多人都写过类似下面的代码。 

 1 import javax.swing.JFrame;
 2 
 3 public class SimpleFrame {
 4     public static void main(String[] args) {
 5         JFrame w = new JFrame();
 6         w.setTitle("SimpleFrame");
 7         w.setSize(400, 300);
 8         w.setLocation(200, 200);
 9         
10         w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11         w.setVisible(true);
12     }
13 }

  代码虽然简单,但是其中还是包含点我不知道的知识点。下面结合自己学习的内容介绍一下WindowConstants接口。

 1 package javax.swing;
 2 
 3 public interface WindowConstants
 4 {   // The do-nothing default window close operation.
 5     public static final int DO_NOTHING_ON_CLOSE = 0;  6     // The hide-window default window close operation.
 7     public static final int HIDE_ON_CLOSE = 1;  8     // The dispose-window default window close operation.
 9     public static final int DISPOSE_ON_CLOSE = 2; 
10     // The exit application default window close operation.
11     public static final int EXIT_ON_CLOSE = 3; 
12 
13 }
常量名代表值含义
DO_NOTHING_ON_CLOSE0关闭窗体时,不执行任何操作  
HIDE_ON_CLOSE1关闭窗体时,窗体自动隐藏
DISPOSE_ON_CLOSE2关闭窗体时,自动隐藏并释放窗体
EXIT_ON_CLOSE3关闭窗体时,退出应用程序(Application非Applet)

   原来这些常量并非是JFrame所特有的,而是在一个特定的接口中定义的。当然,在WindowConstants接口中也有相关说明: The setDefaultCloseOperation and getDefaultCloseOperation methods provided by JFrame, JInternalFrame, and JDialog. 也就是说,JFrame、JInternalFrame和JDialog也实现了WindowConstants接口。

  JFrame中重新定义了 public static final int EXIT_ON_CLOSE = 3; ,因此上面代码中的 JFrame.EXIT_ON_CLOSE 无论无何是合理的。

  ,

转载于:https://www.cnblogs.com/forget406/p/5279969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值