2024年1月24日发(作者:)
Java应用程序窗口关闭的六种方法
Java应用程序窗口关闭的六种方法:
1.使用JFrame的enableEvents和processWindowEvent
/ /
import .*;
import .*;
import .*;
public class Frame1 extends JFrame {
public Frame1() {
enableEvents(_EVENT_MASK);
e(new Dimension(400, 300));
le("Frame1");
}
protected void processWindowEvent(WindowEvent e) {
sWindowEvent(e);
if (() == _CLOSING) {
(0);
}
}
}
2.直接实现WindowListener接口
//
import .*;
import .*;
public class Frame1 extends Frame implements WindowListener {
public Frame1() {
e(new Dimension(400, 300));
le("Frame1");
dowListener(this);
}
public void windowClosing(WindowEventwindowEvent) {
(0);
}
public void windowOpened(WindowEventwindowEvent) { }
public void windowClosed(WindowEventwindowEvent) { }
public void windowIconified(WindowEventwindowEvent) { }
public void windowDeiconified(WindowEventwindowEvent) { }
public void windowActivated(WindowEventwindowEvent) { }
public void windowDeactivated(WindowEventwindowEvent) { }
}
3.直接继承窗体适配器WindowAdapter
//
import .*;
import .*;
public class Frame1 extends WindowAdapter {
public Frame1() {
Frame f=new Frame();
e(new Dimension(400, 300));
le("Frame1");
dowListener(this);
ible(true);
}
public static void main(String[] s){
new Frame1();
}
public void windowClosing(WindowEventwindowEvent) {
(0);
}
}
4.间接继承窗体适配器WindowAdapter
//
import .*;
import .*;
public class Frame1 extends Frame {
public Frame1() {
e(new Dimension(400, 300));
le("Frame1");
dowListener(new winAdapter());
ible(true);
}
public static void main(String[] s){
new Frame1();
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEventwindowEvent) {
(0);
}
}
5.间接实现WindowListener接口
//
import .*;
import .*;
public class Frame1 extends Frame {
public Frame1() {
e(new Dimension(400, 300));
le("Frame1");
dowListener(new winEventHandle());
ible(true);
}
public static void main(String[] s){
new Frame1();
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEventwindowEvent) {
(0);
}
public void windowOpened(WindowEventwindowEvent) { }
public void windowClosed(WindowEventwindowEvent) { }
public void windowIconified(WindowEventwindowEvent) { }
public void windowDeiconified(WindowEventwindowEvent) { }
public void windowActivated(WindowEventwindowEvent) { }
public void windowDeactivated(WindowEventwindowEvent) { }
}
6.使用Inner Class
//
import .*;
import .*;
public class Frame1{
public Frame1(){
Frame f=new Frame();
dowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
(0);
}
});
e(new Dimension(400, 300));
ible(true);
}
public static void main(String[] s){
new Frame1();
}
}
Jframe的关闭方法:
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的关闭方法如下:
dowListener(new Adapter() {
public void windowClosing(Event e) {
(0);
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/news/1706106155a1440908.html
评论列表(0条)