時(shí)間:2015-06-28 00:00:00 來(lái)源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)
1.使用JFrame的enableEvents和processWindowEvent
//Frame1.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame1");
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
2.直接實(shí)現(xiàn)WindowListener接口
//Frame1.java
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends Frame implements WindowListener {
public Frame1() {
this.setSize(new Dimension(400, 300));
this.setTitle("Frame1");
this.addWindowListener(this);
}
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
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) { }
}
3.直接繼承窗體適配器WindowAdapter
//Frame1.java
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends WindowAdapter {
public Frame1() {
Frame f=new Frame();
f.setSize(new Dimension(400, 300));
f.setTitle("Frame1");
f.addWindowListener(this);
f.setVisible(true);
}
public static void main(String[] s){
new Frame1();
}
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
}
#p#副標(biāo)題#e#
4.間接繼承窗體適配器WindowAdapter
//Frame1.java
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends Frame {
public Frame1() {
this.setSize(new Dimension(400, 300));
this.setTitle("Frame1");
this.addWindowListener(new winAdapter());
this.setVisible(true);
}
public static void main(String[] s){
new Frame1();
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
}
5.間接實(shí)現(xiàn)WindowListener接口
//Frame1.java
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends Frame {
public Frame1() {
this.setSize(new Dimension(400, 300));
this.setTitle("Frame1");
this.addWindowListener(new winEventHandle());
this.setVisible(true);
}
public static void main(String[] s){
new Frame1();
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
public void windowOpened(WindowEvent windowEvent) { }
public voi
d windowClosed(WindowEvent windowEvent) { }
public void windowIconified(WindowEvent windowEvent) { }
public void windowDeiconified(WindowEvent windowEvent) { }
public void windowActivated(WindowEvent windowEvent) { }
public void windowDeactivated(WindowEvent windowEvent) { }
}
6.使用Inner Class
//Frame1.java
import java.awt.*;
import java.awt.event.*;
public class Frame1{
public Frame1(){
Frame f=new Frame();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(new Dimension(400, 300));
f.setVisible(true);
}
public static void main(String[] s){
new Frame1();
}
}
Jframe的關(guān)全國(guó)計(jì)算機(jī)等級(jí)考試網(wǎng),加入收藏閉方法:
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的關(guān)閉方法如下:
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
關(guān)鍵詞標(biāo)簽:Java
相關(guān)閱讀
熱門文章 eclipse中如何設(shè)置字體大小_eclipse字體大小設(shè)置方法 jsp 實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì) Eclipse優(yōu)化設(shè)置教程_Eclipse卡頓優(yōu)化設(shè)置技巧 JS截取字符串常用方法詳細(xì)整理
人氣排行 JS驗(yàn)證日期格式是否正確 Java中3DES加密解密調(diào)用示例 Java技術(shù)-J2EE開發(fā)日記-MyEclipse快捷鍵與插件大全 eclipse中如何設(shè)置字體大小_eclipse字體大小設(shè)置方法 Eclipse優(yōu)化設(shè)置教程_Eclipse卡頓優(yōu)化設(shè)置技巧 JavaScript基本語(yǔ)法-常量和變量 用Java刪除文件夾里的所有文件 100多個(gè)很有用的JavaScript函數(shù)以及基礎(chǔ)寫法匯總