java关闭窗体的六种方法 前段时间集中精力写了两篇论文 很久没写博文了 现在继续了
使用JFrame的enableEvents和processWindowEvent
//Frame java
import java awt *;
import java awt event *;
import javax swing *;
public class Frame extends JFrame {
public Frame () {
enableEvents(AWTEvent WINDOW_EVENT_MASK);
this setSize(new Dimension( ));
this setTitle( Frame );
}
protected void processWindowEvent(WindowEvent e) {
super processWindowEvent(e);
if (e getID() == WindowEvent WINDOW_CLOSING) {
System exit( );
}
}
}
直接实现WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extends Frame implements WindowListener {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(this);
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
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) {}
}
直接继承窗体适配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsWindowAdapter {
public Frame () {
Frame f=new Frame();
f setSize(new Dimension( ));
f setTitle( Frame );
f addWindowListener(this);
f setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
间接继承窗体适配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsFrame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winAdapter());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
间接实现WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extendsFrame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winEventHandle());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
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) {}
}
使用Inner Class
//Frame java
import java awt *;
import java awt event *;
public class Frame {
public Frame (){
Frame f=new Frame();
f addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System exit( );
}
});
f setSize(new Dimension( ));
f setVisible(true);
}
【java关闭窗体代码6的简单介绍】
推荐阅读
- 手机申请西瓜视频直播,申请西瓜视频直播条件
- u盘pe系统怎么变回去,pe优盘怎么恢复成正常的优盘
- 经营买卖的游戏,倒卖经营类游戏
- 钉钉的直播提醒怎么开的,钉钉如何直播提醒
- mysql驱动怎么xie的简单介绍
- c语言输出一个数倒过来,c语言将一个数倒着输出
- php循环数据库 php循环函数
- css背景图片渐隐,css实现背景渐变
- u盘启动盘怎么恢复,u盘启动盘怎么恢复liunx系统