How to programmatically close a JFrame

前端 未结 17 1179
深忆病人
深忆病人 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:22

    If you really do not want your application to terminate when a JFrame is closed then,

    use : setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    instead of : setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Here's a synopsis of what the solution looks like,

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

提交回复
热议问题