看了孙鑫老师的视频:“java从入门到精通 ”,里面介绍了通过增加一个窗口监听器,实现窗口的关闭的方法。但是对其具体实现过程,我有些疑问。Interface WindowListenervoid windowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window.
void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on the window.
void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's system menu.
void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.
void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.
void windowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.
void windowOpened(WindowEvent e)
Invoked the first time a window is made visible. 这个监听器含有许多方法。当我们点击关闭按钮时,系统怎么知道调用哪一个方法,我们的这个点击事件具体又是如何传递的?具体由哪个类传递的,能找到相关的源代码吗?
Invoked when the Window is set to be the active Window.
void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on the window.
void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's system menu.
void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.
void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.
void windowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.
void windowOpened(WindowEvent e)
Invoked the first time a window is made visible. 这个监听器含有许多方法。当我们点击关闭按钮时,系统怎么知道调用哪一个方法,我们的这个点击事件具体又是如何传递的?具体由哪个类传递的,能找到相关的源代码吗?
public void windowClosing(WindowEvent e) { System.out.println("colsing has been carryied out");
System.exit(0); }
});但是,一旦执行了windowClosing方法,不管什么情况下,不管你加了什么判断,都会关闭窗口的,同时会执行windowClosing中的代码如果要当用户点关闭按钮后,来根据你的提示是否真正的关闭,应该像下面这样复写processWindowEvent方法,如下:public void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
if (JOptionPane.showConfirmDialog(this, "确定关闭?", "你确定要关闭吗?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
this.dispose();
super.processWindowEvent(e);
System.exit(0);
}
}
}
要知道产生接口的对象必须实现借口所有的类
而WindowAdapter是适配器类 它是实现了WindowListener的所有方法且方法全部为空 方便大家去产生对象来改写需要的方法 所以一般都用WindowAdapter代替WindowListner类class Frametest
{
public static void main(String args[])
{
new Frametest();
}
public Frame()
{
Frame f=new Frame();
f.setSize(200,200);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
}