我想在窗口中有一个按钮,以便如果单击它(按钮),则窗口将关闭.
I would like to have a button in my window such that if I click it (button) the window is closed.
我发现我可以通过以下方式关闭窗口:
I found out that I can close a window in the following way:
referenceToTheFrame.hide(); //hides the frame from view refToTheFrame.dispose(); //disposes the frame from memory但是,如果我这样做,编译器会抱怨:
But if I do this way, compiler complains:
Note: myProgram.java uses or overrides a deprecated API Note: Recompile with -Xlint:deprication for details.我做不安全的事吗?
推荐答案推荐的使用方法是:
referenceToTheFrame.setVisible(false)hide方法已被弃用,不应再使用. (尽管在内部setVisible会调用hide或show)
The hide method is deprecated and should not longer be used. (Although internally setVisible will call hide or show)
此外,如果要放置框架,则必须手动调用dispose. (例如,如果您需要关闭窗口的事件)对setDefaultCloseOperation的调用对您无济于事,因为它只会影响在系统菜单上单击关闭按钮后的行为.
Additionally if you want to dispose the frame, you have to call dispose manually. (In case you need the window-closed event for example) A call to setDefaultCloseOperation doesn't help you here, because it only affects the behavior after a click on the close button from the system menu.
请参阅 Swing文档进一步的信息.
更多推荐
如何通过单击按钮关闭JFrame?
发布评论