How to close a JFrame by clicking a button?

I would like to have a button in my window so that if I press a button (button), the window will be closed.

I found out that I can close the window as follows:

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

But if I do this, the compiler complains:

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

Am I doing something unsafe?

+3
source share
3 answers

Recommended Method:

referenceToTheFrame.setVisible(false)

The method is hidedeprecated and should no longer be used. (Although inside setVisiblewill call hideor show)

, , dispose . (, ). setDefaultCloseOperation , .

. Swing.

+10

DR . : hide() - , Window. , setVisible().

+2

dispose() .

+2

Source: https://habr.com/ru/post/1740311/


All Articles