簡體   English   中英

如何通過單擊按鈕關閉JFrame?

[英]How can I close a JFrame by click on a button?

我想在我的窗口中有一個按鈕,如果我點擊它(按鈕),窗口就會關閉。

我發現我可以通過以下方式關閉窗口:

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

但如果我這樣做,編譯器抱怨:

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

我做一些不安全的事嗎?

推薦的方法是使用:

referenceToTheFrame.setVisible(false)

不推薦使用hide方法,不應再使用。 (雖然內部setVisible會調用hideshow

此外,如果要丟棄框架,則必須手動調用dispose (例如,如果您需要窗口關閉事件)調用setDefaultCloseOperation對此沒有幫助,因為它只影響從系統菜單中單擊關閉按鈕后的行為。

有關詳細信息,請參閱Swing文檔

DR有正確的答案。 關於編譯器警告: hide()是屬於Window的不推薦的調用。 不要使用它,堅持使用setVisible()。

在frame對象上使用dispose()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM