Java swing. How to wait for other Jframes

妖精的绣舞 提交于 2019-12-31 01:44:38

问题


In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.

How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?


回答1:


Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed

see http://mindprod.com/jgloss/modal.html for explanation and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example

If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..




回答2:


I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:

How do I make one JFrame wait for another JFrame?

I would say the easiest way to acheive this is by registering and firing event listeners.

  • In your "child" frame, register the "main" frame as an event listener.
  • In your "main" frame,
    • implement your choice of listener, e.g. ActionListener
    • in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.

One can easily implement this to a ny number of situations, including the login scenario described in the question.




回答3:


Use JModalFrame instead of JFrame.



来源:https://stackoverflow.com/questions/1812012/java-swing-how-to-wait-for-other-jframes

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