import java.awt.*;
import java.awt.event.*;
public class TestFrame3 implements ActionListener{//监听器的实现
Frame f=new Frame("IT人咨询交流网");//成员变量
public static void main(String args[]){
TestFrame3 tf=new TestFrame3();
tf.init();

}
public void init(){
Button btn=new Button("退出");
btn.addActionListener(new TestFrame3());
 f.addWindowListener(new MyWindowListener());
f.add(btn);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
f.setVisible(false);
f.dispose();
System.exit(0);
}}
 class MyWindowListener extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);

}

我的本意是在f上注册一个窗口监听器,在button上注册一个监听器.可是好像只有button上的可以起作用.
原来的程序如下:可以得到结果
import java.awt.*;
import java.awt.event.*;
public class TestFrame2 implements ActionListener {
Frame f=new Frame("IT人咨询交流网");
public static void main(String []args){
TestFrame2 tf=new TestFrame2();
Button btn=new Button("退出");
btn.addActionListener(new TestFrame2());
tf.f.addWindowListener(new MyWindowListener());
tf.f.add(btn);
tf.f.setSize(300,200);
tf.f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
f.setVisible(false);
f.dispose();
System.exit(0);
}
}
class MyWindowListener extends WindowAdapter
{

public void windowClosing(WindowEvent e){
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}

}

解决方案 »

  1.   

    呵呵,经过我的认真排除.终于发现第一个程序中的事件处理器中的消息处理方法windowClosing(WindowEvent e)写成了大写的WindowClosing.这个样子的话,就不能覆盖WindowAdapter的那个什么也不做的windowClosing方法.也就不能够实现预先设想的功能.因为这个方法成了一个"死"的,没有用.