欢迎来到天天文库
浏览记录
ID:40749586
大小:34.00 KB
页数:4页
时间:2019-08-07
《Java关闭窗体方法总结》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Java关闭窗体方法总结 1.使用JFrame的enableEvents和processWindowEvent //Frame1.java importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; publicclassFrame1extendsJFrame{ publicFrame1(){ enableEvents(AWTEvent.WINDOW_EVENT_MASK); this.setSize(newDimen
2、sion(400,300)); this.setTitle("Frame1"); } protectedvoidprocessWindowEvent(WindowEvente){ super.processWindowEvent(e); if(e.getID()==WindowEvent.WINDOW_CLOSING){ System.exit(0); } } } 2.直接实现WindowListener接口 //Frame1.java importjava.awt.*;
3、importjava.awt.event.*; publicclassFrame1extendsFrameimplementsWindowListener{ publicFrame1(){ this.setSize(newDimension(400,300)); this.setTitle("Frame1"); this.addWindowListener(this); } publicvoidwindowClosing(WindowEventwindowEvent){ System
4、.exit(0); } publicvoidwindowOpened(WindowEventwindowEvent){} publicvoidwindowClosed(WindowEventwindowEvent){} publicvoidwindowIconified(WindowEventwindowEvent){} publicvoidwindowDeiconified(WindowEventwindowEvent){} publicvoidwindowActivated(Wind
5、owEventwindowEvent){} publicvoidwindowDeactivated(WindowEventwindowEvent){} } 3.直接继承窗体适配器WindowAdapter //Frame1.java importjava.awt.*; importjava.awt.event.*; publicclassFrame1extendsWindowAdapter{ publicFrame1(){ Framef=newFrame(); f.setSize
6、(newDimension(400,300)); f.setTitle("Frame1"); f.addWindowListener(this); f.setVisible(true); } publicstaticvoidmain(String[]s){ newFrame1(); } publicvoidwindowClosing(WindowEventwindowEvent){ System.exit(0); } } 4.间接继承窗体适配器WindowAdapter /
7、/Frame1.java importjava.awt.*; importjava.awt.event.*; publicclassFrame1extendsFrame{ publicFrame1(){ this.setSize(newDimension(400,300)); this.setTitle("Frame1"); this.addWindowListener(newwinAdapter()); this.setVisible(true); } publicstatic
8、voidmain(String[]s){ newFrame1(); } } classwinAdapterextendsWindowAdapter{ publicvoidwindowClosing(WindowEventwindowEvent){ System.exit(0); } } 5.间接实现WindowListener接口 //Frame1.java importjava.awt.*; importjava.awt.event.*; p
此文档下载收益归作者所有