这是我学下做窗体的一个程序,但是事件总是响应不了,后来我改了一下用内类来监听!
import java.awt.*;
import java.awt.event.*;
 public class SimpleFrame extends Frame{
SimpleFrame(String title)
{
super(title);
MyWindowAdapter adapter = new  MyWindowAdapter(this);
addWindowListener(adapter);
}
public static void main(String[] args) {
Frame f;
f = new SimpleFrame("第一个窗体");
f.setSize(300,200);
  f.setVisible(true);
  }
public void paint(Graphics g)
{
g.drawString("应用窗体为:....",15,100);
}
 }
class MyWindowAdapter extends WindowAdapter{
  SimpleFrame simpleFrame;
public MyWindowAdapter(SimpleFrame frame) {
simpleFrame = frame;
}
  public void windowsClosing(WindowEvent we){
System.out.println("按中了!");
simpleFrame.setVisible(false);
  }
}
以下是修改后的程序!事件能监听了,但是为什么上面程序监听不了,请前辈们指点!
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame extends Frame{
SimpleFrame(String title)
{
super(title);
addWindowListener( new WindowAdapter(){
                         public void windowClosing(WindowEvent e){
                            System.out.println("按中了");
                     System.exit(0);
                  }
                  } );
}
public static void main(String[] args) {
Frame f;
f = new SimpleFrame("第一个窗体");
f.setSize(300,200);
f.setVisible(true);
}
public void paint(Graphics g)
{
g.drawString("应用窗体为:....",15,100);
}
}

解决方案 »

  1.   

    第一例中的windowsClosing 应该是windowClosing. :-)
      

  2.   

    这就是用识配器要特别注意的地方了,你写错了函数名相当于是重新创建了一个函数,而不是override了函数
      

  3.   

    更菜有点问题想问:
    为什么在第一个类SimpleFrame 调用了MyWindowAdapter定义,到MyWindowAdapter类中又可用SimpleFrame 定义对象?(星号表注处) 这不会出错吗? 第一类:public class SimpleFrame extends Frame{
    SimpleFrame(String title)
    {
    super(title);
    ***** MyWindowAdapter adapter = new  MyWindowAdapter(this);  *****第二个:class MyWindowAdapter extends WindowAdapter{
     ******* SimpleFrame simpleFrame;   ********
    public MyWindowAdapter(SimpleFrame frame) {
    simpleFrame = frame;
    }