public class MouseDemo extends JFrame implements MouseListener,MouseMotionListener{ public MouseDemo(){
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
setBackground(Color.white);
setLocation(200,100);
setSize(300,300);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
});
}
        
          public static void main(String[] args){
new MouseDemo();
new MouseDemo();

}
}
上面这段代码new出两个独立的JFrame,我在关闭其中一个frame时为什么两个会被时候关闭。请高手指点!

解决方案 »

  1.   

    System.exit(0); 是整个程序退出 当然都没有了
    要改成dispose方法
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JFrame;public class MouseDemo extends JFrame { public MouseDemo() {
    Container contentPane = getContentPane();
    contentPane.setBackground(Color.white);
    setBackground(Color.white);
    setLocation(200, 100);
    setSize(300, 300);
    setVisible(true);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    MouseDemo.this.dispose();
    }
    });
    } public static void main(String[] args) {
    new MouseDemo();
    new MouseDemo();
    }
    }
      

  2.   

    楼上的说的对System.exit(0)是整个程序退出,说的更详细些就是exit方法是终止当前java虚拟机的运行。
    从windows的任务管理器中我们可以看到每运行一个java程序系统便会运行一个javaw.exe的进程,个人认为
    这便是一个java虚拟机程序退出了它自然两个窗口会被同时关闭。