Java – how do I close the JFrame by clicking the button?

I want to have a button in my window. If I click it, the window will close

I found that I can close windows in the following ways:

referenceToTheFrame.hide(); //hides the frame from view
refToTheFrame.dispose(); //disposes the frame from memory

But if I do, the compiler complains:

Note: myProgram.java uses or overrides a deprecated API
Note: Recompile with -Xlint:deprication for details.

Do I do something unsafe?

Solution

The recommended method is to use:

referenceToTheFrame.setVisible(false)

The hide method is not recommended and should not be used again (although the internal setvisible will call hide or show)

In addition, if you want to discard the framework, you must manually call dispose (for example, if you need a window close event) calling setdefaultcloseoperation does not help because it only affects the behavior after clicking the close button from the system menu

For details, see swing documentation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>