如何使一个由frame类构成的窗口,按住右上角的“X”按钮,关闭窗口。使用什么的函数,最好是frame类的。大家有什么想法?import java.awt.*;public class BorderLayoutEx{ public static void main(String[] args){
Frame f ;
Button b1,b2,b3,b4,b5;
f = new Frame("BorderLayoutEx");
f.setLayout(new BorderLayout());
b1 = new Button("North");
b2 = new Button("South");
b3 = new Button("East");
b4 = new Button("West");
b5 = new Button("Center");
f.add("North",b1);
f.add("South",b2);
f.add("East",b3);
f.add("West",b4);
f.add("Center",b5);
f.setSize(200,200);
f.setVisible(true);
}
}

解决方案 »

  1.   

    使用JFrame 不行吗?
    JFrame frame = new JFrame;frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

  2.   


    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;public class DDD { public static void main(String[] args) {
    Frame f;
    Button b1, b2, b3, b4, b5;
    f = new Frame("BorderLayoutEx");
    f.setLayout(new BorderLayout());
    b1 = new Button("North");
    b2 = new Button("South");
    b3 = new Button("East");
    b4 = new Button("West");
    b5 = new Button("Center");
    f.add("North", b1);
    f.add("South", b2);
    f.add("East", b3);
    f.add("West", b4);
    f.add("Center", b5);
    f.setSize(200, 200);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    });
    }
    }
      

  3.   

    Jframe.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) 
    {
                                    // EXECUTE
    System.exit(0);
    }
    });
    是这样吗?
      

  4.   

    楼主我前几天正好了写了个 
    是这样的import java.awt.*; 
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;public class BorderLayoutEx{ public static void main(String[] args){ 
    Frame f ; 
    Button b1,b2,b3,b4,b5; 
    f = new Frame("BorderLayoutEx"); 
    f.setLayout(new BorderLayout()); 
    b1 = new Button("North"); 
    b2 = new Button("South"); 
    b3 = new Button("East"); 
    b4 = new Button("West"); 
    b5 = new Button("Center"); 
    f.add("North",b1); 
    f.add("South",b2); 
    f.add("East",b3); 
    f.add("West",b4); 
    f.add("Center",b5); 
    f.setSize(200,200); 
    f.addWindowListener(new WindowAdapter(){   //使用内部类 用WindowAdapter这个类
    public void windowClosing(WindowEvent e){  //使用windowClosing这个方法 
     System.exit(0);
    }
    });
    f.setVisible(true); 

    }
    楼主不明白的话可以查API
      

  5.   

    也许你对内部类不了解
    还有一个方法 不用内部类的 看起来比较容易理解import java.awt.*; 
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;public class BorderLayoutEx{ public static void main(String[] args){ 
    Frame f ; 
    Button b1,b2,b3,b4,b5; 
    f = new Frame("BorderLayoutEx"); 
    f.setLayout(new BorderLayout()); 
    b1 = new Button("North"); 
    b2 = new Button("South"); 
    b3 = new Button("East"); 
    b4 = new Button("West"); 
    b5 = new Button("Center"); 
    f.add("North",b1); 
    f.add("South",b2); 
    f.add("East",b3); 
    f.add("West",b4); 
    f.add("Center",b5); 
    f.setSize(200,200);
    Monitior m = new Monitior(); //new一个类
    f.addWindowListener(m); //添加监听类
    f.setVisible(true); 

    }class Monitior extends WindowAdapter  {
    public void windowClosing(WindowEvent e){   //使用Closing方法
    System.exit(0);
    }
    }
      

  6.   

    1.用swing的jframe
    2.写内部类 就像楼上写的一样
      

  7.   

       f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent we){
                    System.exit(0);