setDefaultCloseOperation(JFrame.EXIT_ON_CLOES)會(huì)讓整個(gè)程序都退出
成都創(chuàng)新互聯(lián)是一家專業(yè)提供隆林企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、H5建站、小程序制作等業(yè)務(wù)。10年已為隆林眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
JFrame.DISPOSE_ON_CLOSE只讓當(dāng)前的窗口關(guān)閉而已
前段時(shí)間集中精力寫(xiě)了兩篇論文 很久沒(méi)寫(xiě)博文了 現(xiàn)在繼續(xù)了
使用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( );
}
}
}
直接實(shí)現(xiàn)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 extends? WindowAdapter {
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 extends? Frame {
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( );
}
}
間接實(shí)現(xiàn)WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extends? Frame {
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);
}
public static void main(String[] s){
new Frame ();
}
}
Jframe的關(guān)閉方法
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的關(guān)閉方法如下
this addWindowListener(new java awt event WindowAdapter() {
public void windowClosing(java awt event WindowEvent e) {
System exit( );
}
lishixinzhi/Article/program/Java/hx/201311/27073
給Frame 添加一個(gè)監(jiān)聽(tīng)就可以了,如果你是用的JFrame不用添加監(jiān)聽(tīng)直接點(diǎn)擊小叉叉就可以關(guān)閉窗口了
Frame?frame?=?new?Frame("測(cè)試");
frame.setSize(300,?300);
frame.setVisible(true);
frame.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);//?退出程序
}
});