我是JAVA的初学者,有个问题请教:
如果实例化两个JFrame窗口,在这个两窗口中分别加两个“退出”按钮,我希望点击一个
窗口中的“退出”按钮时仅退出本窗口而不退出程序,应该用什么方法呢?
System.exit(0);肯定是不行了,该用什么呢

解决方案 »

  1.   

    System.exit(0);这是JVM退出运行,当然整个程序结束运行啦.要退出JFrame,在事件监听处理的时候调用dispose()方法就行了.
      

  2.   

    不要把这句话加进去setDefaultCloseOperation(3);就不会关掉了!
      

  3.   

    同意楼上的
    也可以写成setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

  4.   

    dispose()或者setVisible(false)也可以达到效果吧
      

  5.   

    setDefaultCloseOperation(JFrame_EXIT_ON_CLOSE)应该可以的
      

  6.   

    他是说不要退出
    应该是setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
      

  7.   

    setVisible(false)就好了吧,只是关闭掉,不从内存中删除
      

  8.   

    setVisible(false)不好,他是说退出窗口,那还是要把内存释放掉的!
      

  9.   

    setVisible(false);
    dipose();不要把这句话加进去setDefaultCloseOperation(3);就不会关掉了!
      

  10.   

    dispose应该是销毁了吧,setVisibel就行了
      

  11.   

    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
      

  12.   

    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
      

  13.   

    dispose();
    ------------------------------------------------------------------PC火烈鸟
    http://blog.csdn.net/pcfirebird/
      

  14.   

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

  15.   

    试了一下,当调用dispose()方法后,frame对象并不从内存中释放,还保留着原来的对象,这时再调用frame.show()原来的窗体就又出来了,所以我个人认为dispose方法并不能释放对象
      

  16.   

    这样的话,setVisible(false)和dispose();这两个方法没有什么区别?
    JDK1.5这样说:
    dispose
    public void dispose()
    Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be ed as undisplayable. 
    The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions). setVisible
    public void setVisible(boolean b)
    Shows or hides this component depending on the value of parameter b. 
      

  17.   

    dispose:关闭窗口并收回用于创建窗口的任何系统资源
    void setvisible(boolean b)根据b的取值来决定显示还是隐藏