java swing close window without exiting app

后端 未结 8 1459
慢半拍i
慢半拍i 2021-01-12 04:31

I have a little frame where I ask user & password. This frame will be opened clicking over a button in a main window.

Then I have two buttons: ok and cancel.

相关标签:
8条回答
  • 2021-01-12 05:01

    You can do it in many ways but these two ways are most usable one
    1. write this.setVisible(false) in inside implemented ActionListener
    Or
    2. write this.dispose() inside implemented ActionListener. Hope this will help you.

    0 讨论(0)
  • 2021-01-12 05:05

    Make sure you do not:

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    0 讨论(0)
  • 2021-01-12 05:10

    Make a function in outer class where you are implementing the JFrame (you need to close on pressing cancel button).
    Write this.setVisible(false); in the implementation of that function.
    Finally call this function in the ActionListener implementation when you want to close it.

    0 讨论(0)
  • 2021-01-12 05:11

    Maybe a cleaner way is just change the setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE :

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    0 讨论(0)
  • 2021-01-12 05:11

    setVisible method does not release memory resources and should be used only when the form is to be used again.

    The dispose method 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 marked as undisplayable.

    0 讨论(0)
  • 2021-01-12 05:19

    You can use either Frame.hide() or Frame.dispose(). I would also recommend to look into JDialog or JOptionPane

    Correction: hide() is deprecated. SetVisible(false) should be used instead

    0 讨论(0)
提交回复
热议问题