java swing close window without exiting app

末鹿安然 提交于 2019-12-04 01:01:38

问题


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.

When I click on "cancel" button, I need to close this frame without exiting the app.

How can I do that?


回答1:


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




回答2:


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

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);



回答3:


You can call setVisible(false) on the frame.

You might also want to call setDefaultCloseOperation on the frame passing in HIDE_ON_CLOSE (info here: http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29). That will prevent the app from going away if they user hits the "X" on the JFrame to close it.




回答4:


Use this.dispose(); in the action listener method when the username/password succeeds. eg:

public void actionPerformed(ActionEvent ae) {

    if(ae.getSource()=="button you press to confirm username/password"){
        if(userNameTf.getText().equals(username)&&isPassword(passwordTf.getPassword())){
            new "window to be opened upon success"
            this.dispose(); // calls dispose on this object ie. 
                                            // the login window 
        }
        else{
            userNameTf.setText("");
            passwordTf.setText("");

            JOptionPane.showMessageDialog(this,
                    "Username and/or password is incorrect!",
                    "Attention!",
                    JOptionPane.WARNING_MESSAGE);
        }
    }

}

If you are using inner classes to handle the events just replace 'this.dispose()' with Super_Class_Name.this.dispose();




回答5:


Make sure you do not:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



回答6:


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.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/573317/java-swing-close-window-without-exiting-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!