问题描述
我有2个JFrame,一个是主JFrame,另一个是子JFrame,并且当我显示子JFrame时,我试图使用户交互无法访问主JFrame.
I have 2 JFrames one is the main JFrame and the other one is a sub JFrame, and I'm trying to make the main JFrame inaccessible to user interactions when I display the sub JFrame.
推荐答案
不要.使用模式JDialog-正是它们的用途.您当然知道JDialog可以容纳复杂的GUI,就像JFrame所容纳的GUI一样复杂.
Don't. Use a modal JDialog -- that's precisely what they're for. You understand of course that a JDialog can hold a complex GUI, as complex as any held by a JFrame.
我们经常碰到这样的人,他们使用诸如NetBeans这样的GUI生成器来帮助他们创建GUI,并且由于第二个窗口的代码是由该生成器创建的,以扩展JFrame,所以对于程序员来说很难返回并更改为对话框.解决此问题的方法是尝试使您的Swing代码创建更适合创建JPanels,而不是JFrames之类的顶级窗口.这样,您可以在需要的情况下在JFrame中使用创建的JPanel,在需要时在JDialog中使用,也可以在JApplet中使用JApplet,甚至可以使用其他JPanel,无论哪种情况最适合这种情况.这将极大地增加代码的灵活性.
We often run into posts like these by folks who use a GUI-builder such as NetBeans to help them create their GUI's, and since the second window's code was created by the builder to extend a JFrame, it's very hard for the programmer to go back and change it to a dialog. The way to fix this is to try to gear your Swing code creation towards creation of JPanels, not top-level windows such as JFrames. This way you could use the created JPanel in a JFrame if desired, a JDialog if desired, a JApplet, or even another JPanel, whatever works best for the situation. This will increase your code's flexibility tremendously.
这篇关于如何制作模态JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!