java swing关闭窗口而不退出应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/573317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java swing close window without exiting app
提问by Giancarlo
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?
我怎样才能做到这一点?
采纳答案by Itay Maman
You can use either Frame.hide() or Frame.dispose(). I would also recommend to look into JDialog or JOptionPane
您可以使用 Frame.hide() 或 Frame.dispose()。我还建议查看 JDialog 或 JOptionPane
Correction: hide() is deprecated. SetVisible(false) should be used instead
更正:hide() 已弃用。应该使用 SetVisible(false) 代替
回答by TofuBeer
You can call setVisible(false) on the frame.
您可以在框架上调用 setVisible(false)。
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.
您可能还想在传递 HIDE_ON_CLOSE 的框架上调用 setDefaultCloseOperation (信息在这里:http: //java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29)。如果用户点击 JFrame 上的“X”关闭它,这将防止应用程序消失。
回答by jedierikb
Make sure you do not:
确保您不会:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
回答by Anna
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.
在您正在实现的外部类中创建一个函数JFrame
(您需要按取消按钮关闭)。
写入this.setVisible(false);
该函数的实现。
最后ActionListener
当你想关闭它时,在实现中调用这个函数。
回答by aleroot
Maybe a cleaner way is just change the setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE :
也许更简洁的方法是将 setDefaultCloseOperation 从 EXIT_ON_CLOSE 更改为 DISPOSE_ON_CLOSE :
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
回答by Alan
Use this.dispose();
in the action listener method when the username/password succeeds. eg:
this.dispose();
当用户名/密码成功时在动作侦听器方法中使用。例如:
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();
如果您使用内部类来处理事件,只需将 'this.dispose()' 替换为 Super_Class_Name.this.dispose();
回答by Qadir Hussain
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.
您可以通过多种方式进行操作,但这两种方式是最有用的一种
1.this.setVisible(false)
在已实现的内部
写入
或2.在已实现的内部写入。希望这会帮助你。ActionListener
this.dispose()
ActionListener
回答by Edgard Leal
setVisible method does not release memory resources and should be used only when the form is to be used again.
setVisible 方法不会释放内存资源,只有在再次使用表单时才应使用。
The disposemethod 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.
该处置方法释放由此Window,其子组件所使用的本机屏幕资源,其拥有的所有儿童。也就是说,这些组件的资源将被销毁,它们消耗的任何内存都将返回给操作系统,并且它们将被标记为不可显示。