How to make a modal JFrame? [duplicate]

此生再无相见时 提交于 2019-12-20 03:21:44

问题


Possible Duplicate:
How to make a JFrame Modal in Swing java

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.


回答1:


How to make a modal JFrame?

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.

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.




回答2:


use JDialog and send the third parameter to its constructor as true (modal)



来源:https://stackoverflow.com/questions/8351505/how-to-make-a-modal-jframe

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