java

位置:IT落伍者 >> java >> 浏览文章

java关闭窗体的六种方法


发布日期:2021年06月03日
 
java关闭窗体的六种方法

前段时间集中精力写了两篇论文很久没写博文了现在继续了

使用JFrame的enableEvents和processWindowEvent

//Framejava

import javaawt*;

import javaawtevent*;

import javaxswing*;

public class Frame extends JFrame {

public Frame() {

enableEvents(AWTEventWINDOW_EVENT_MASK);

thissetSize(new Dimension( ));

thissetTitle(Frame);

}

protected void processWindowEvent(WindowEvent e) {

superprocessWindowEvent(e);

if (egetID() == WindowEventWINDOW_CLOSING) {

Systemexit();

}

}

}

直接实现WindowListener接口

//Framejava

import javaawt*;

import javaawtevent*;

public class Frame extends Frame implements WindowListener {

public Frame() {

thissetSize(new Dimension( ));

thissetTitle(Frame);

thisaddWindowListener(this);

}

public void windowClosing(WindowEvent windowEvent) {

Systemexit();

}

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

//Framejava

import javaawt*;

import javaawtevent*;

public class Frame extends WindowAdapter {

public Frame() {

Frame f=new Frame();

fsetSize(new Dimension( ));

fsetTitle(Frame);

faddWindowListener(this);

fsetVisible(true);

}

public static void main(String[] s){

new Frame();

}

public void windowClosing(WindowEvent windowEvent) {

Systemexit();

}

}

间接继承窗体适配器WindowAdapter

//Framejava

import javaawt*;

import javaawtevent*;

public class Frame extends Frame {

public Frame() {

thissetSize(new Dimension( ));

thissetTitle(Frame);

thisaddWindowListener(new winAdapter());

thissetVisible(true);

}

public static void main(String[] s){

new Frame();

}

}

class winAdapter extends WindowAdapter{

public void windowClosing(WindowEvent windowEvent) {

Systemexit();

}

}

间接实现WindowListener接口

//Framejava

import javaawt*;

import javaawtevent*;

public class Frame extends Frame {

public Frame() {

thissetSize(new Dimension( ));

thissetTitle(Frame);

thisaddWindowListener(new winEventHandle());

thissetVisible(true);

}

public static void main(String[] s){

new Frame();

}

}

class winEventHandle implements WindowListener {

public void windowClosing(WindowEvent windowEvent) {

Systemexit();

}

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

//Framejava

import javaawt*;

import javaawtevent*;

public class Frame{

public Frame(){

Frame f=new Frame();

faddWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

Systemexit();

}

});

fsetSize(new Dimension( ));

fsetVisible(true);

}

public static void main(String[] s){

new Frame();

}

}

Jframe的关闭方法

setDefaultCloseOperation(EXIT_ON_CLOSE);

frame的关闭方法如下

thisaddWindowListener(new javaawteventWindowAdapter() {

public void windowClosing(javaawteventWindowEvent e) {

Systemexit();

}

});

               

上一篇:在Java程序中截获控制台输出

下一篇:Java中的构造函数继承