import java.awt.*;
import java.awt.event.*;public class TestWindowClose {
public static void main (String[] args) {
}
}class MyFrame extends Frame{
MyFrame(String s){
super(s);
setLayout(null);
setBounds(300,300,400,300);
this.setBackground(new Color(204,204,255));
setVisible(true);

this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(-1);
}
}
);
}
}
为什么运行没有界面,好像窗口直接就关闭了

解决方案 »

  1.   

    MyFrame构造函数里面写
    setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    就可以在窗口关闭的时候关掉进程了吧
      

  2.   


    ++main 方法里  创建一个MyFrame 对象  MyFrame m=new MyFrame();
      

  3.   


    import java.awt.*;
    import java.awt.event.*;public class TestWindowClose {
        public static void main (String[] args) {
            String s = null;
         MyFrame myFrame=new MyFrame(s);
        }
    }class MyFrame extends Frame{
        MyFrame(String s){
            super(s);
            setLayout(null);
            setBounds(300,300,400,300);
            this.setBackground(new Color(204,204,255));
            setVisible(true);
        
            this.addWindowListener(
                new WindowAdapter(){
                    public void windowClosing(WindowEvent e){
                        setVisible(false);
                        System.exit(-1);
                    }
                }
                    );
        }
    }