How to programmatically close a JFrame

前端 未结 17 1315
深忆病人
深忆病人 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:30

    Based on the answers already provided here, this is the way I implemented it:

    JFrame frame= new JFrame()
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // frame stuffs here ...
    
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    

    The JFrame gets the event to close and upon closing, exits.

提交回复
热议问题