Disposing a jFrame usinga button from another jFrame in netbeans [duplicate]

爷,独闯天下 提交于 2019-12-25 14:12:53

问题


Is it possible to dispose a jFrame using a button of another jFrame? If it is possible, how should the codes look like in netbeans?


回答1:


Is it possible to dispose a jFrame using a button of another jFrame?

Yes.

If it is possible, how should the codes look like in netbeans?

In the JButton's ActionListener:

myOtherFrame.dispose();

Your question in a nutshell is essentially -- how can I call a method on one object from another object, that's it. And the key to doing this is to be sure that you have a decent reference to the object whose behavior you'd like to change (whose method you'd like to call) inside the calling object. So here you'd need a decent reference to the displayed window in order to call a method on it.

Likely my recommendations won't help you all that much since the key to all this is the structure of your program, including the references that you have for the one Jframe in the other class, details that you've not shown or told us.

Also, it is very unlikely that you even want to have more than one JFrame visible at a time as most professional applications have at most one top-level main window (for Swing this is a JFrame) and sometimes have child windows which for Swing would be JDialogs.

Please see: The Use of Multiple JFrames, Good/Bad Practice?.



来源:https://stackoverflow.com/questions/32994935/disposing-a-jframe-usinga-button-from-another-jframe-in-netbeans

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