Java 父子 JFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16952851/
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 Parent and Child JFrame
提问by Jin Ginusuke
I'm new to javax.swing.*
packages and only familiar to c#.net.
I have two JFrames namely frmLogin
and frmMain
respectively.
All I want to do is a simple code something like this:
我是javax.swing.*
包的新手,只熟悉 c#.net。我有即两个JFramesfrmLogin
和frmMain
分别。我想要做的只是一个简单的代码,如下所示:
+----- C# version ---------
+----- C# 版本---------
this.hide();
new frmMain().ShowDialog();
this.show();
tbPsswrd.Text = String.empty;
+---- JAVA version -------
+---- JAVA 版 -------
this.setVisible(false);
new frmMain().setVisible(true);
this.setVisible(true);
tbPsswrd.Text = "";
+---------------------------
+----------------------------
In Java JFrame, only show()
and setVisible(boolean isVisible)
is available,
hence even though I'm not finished with frmMain()
, it will continue to do the
rest of the code next to it (which is to show the parent form and empty the password
box).
在 Java JFrame 中,只有show()
和setVisible(boolean isVisible)
可用,因此即使我没有完成frmMain()
,它也会继续执行旁边的其余代码(即显示父表单并清空密码框)。
In summary, I want an equivalent functionality of method .showDialog();
not just .show();
so that it wont continue to the next codes unless I closed the child form.
总而言之,我想要一个等效的方法功能,.showDialog();
不仅.show();
如此,除非我关闭子窗体,否则它不会继续执行下一个代码。
Thanks in advance! ü
提前致谢!ü
回答by DannyMo
Use a modal JDialog
instead of a JFrame
and dialog.setVisible(true)
will block until the dialog is closed. For example:
使用 modalJDialog
而不是 aJFrame
并且dialog.setVisible(true)
会阻塞直到对话框关闭。例如:
JDialog dialog = new JDialog(parentFrame, true); // parent, isModal
dialog.setVisible(true); // blocks until dialog is closed
// ... do stuff after dialog is closed