How to programmatically close a JFrame

前端 未结 17 962
深忆病人
深忆病人 2020-11-22 05:42

What\'s the correct way to get a JFrame to close, the same as if the user had hit the X close button, or pressed Alt+F4 (on W

17条回答
  •  情歌与酒
    2020-11-22 06:17

    If you want the GUI to behave as if you clicked the X close button then you need to dispatch a window closing event to the Window. The ExitAction from Closing An Application allows you to add this functionality to a menu item or any component that uses Actions easily.

    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    

提交回复
热议问题