How to programmatically close a JFrame

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

    You have to insert the call into the AWT message queue so all the timing happens correctly, otherwise it will not dispatch the correct event sequence, especially in a multi-threaded program. When this is done you may handle the resulting event sequence exactly as you would if the user has clicked on the [x] button for an OS suppled decorated JFrame.

    public void closeWindow()
    {
        if(awtWindow_ != null) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    awtWindow_.dispatchEvent(new WindowEvent(awtWindow_, WindowEvent.WINDOW_CLOSING));
                }
            });
        }
    }
    

提交回复
热议问题