How to programmatically close a JFrame

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

    If you do not want your application to terminate when a JFrame is closed, use: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

    instead of: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    From the documentation:

    DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.

    HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.

    DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.

    EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

    might still be useful: You can use setVisible(false) on your JFrame if you want to display the same frame again. Otherwise call dispose() to remove all of the native screen resources.

    copied from Peter Lang

    https://stackoverflow.com/a/1944474/3782247

    0 讨论(0)
  • 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));
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:16
    setVisible(false); //you can't see me!
    dispose(); //Destroy the JFrame object
    

    Not too tricky.

    0 讨论(0)
  • 2020-11-22 06:16
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-11-22 06:20

    This examples shows how to realize the confirmed window close operation.

    The window has a Window adapter which switches the default close operation to EXIT_ON_CLOSEor DO_NOTHING_ON_CLOSE dependent on your answer in the OptionDialog.

    The method closeWindow of the ConfirmedCloseWindow fires a close window event and can be used anywhere i.e. as an action of an menu item

    public class WindowConfirmedCloseAdapter extends WindowAdapter {
    
        public void windowClosing(WindowEvent e) {
    
            Object options[] = {"Yes", "No"};
    
            int close = JOptionPane.showOptionDialog(e.getComponent(),
                    "Really want to close this application?\n", "Attention",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    options,
                    null);
    
            if(close == JOptionPane.YES_OPTION) {
               ((JFrame)e.getSource()).setDefaultCloseOperation(
                       JFrame.EXIT_ON_CLOSE);
            } else {
               ((JFrame)e.getSource()).setDefaultCloseOperation(
                       JFrame.DO_NOTHING_ON_CLOSE);
            }
        }
    }
    
    public class ConfirmedCloseWindow extends JFrame {
    
        public ConfirmedCloseWindow() {
    
            addWindowListener(new WindowConfirmedCloseAdapter());
        }
    
        private void closeWindow() {
            processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
        }
    }
    
    0 讨论(0)
提交回复
热议问题