Recursion causes exit to exit all JFrames (terminates the application)

I made an application that gives the user the opportunity to fully open a new caviar of the application. When the user does this and closes the application, the entire application terminates; not just a window.

How should I look for the application spawning recursively, and then when the user exits the JFrame calf; killing only JFrame and not the whole instance?

Here is the relevant code:

[...]
JMenuItem newMenuItem = new JMenuItem ("New");
newMenuItem.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
{        
    new MainWindow();
    }
});
fileMenu.add(newMenuItem);

[....]

JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
});
fileMenu.add(exit);
[...]
+3
source share
2 answers

I completely deleted the code frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE);.

DISPOSE_ON_CLOSE, . windowEvent : frame.dispose();, - , .

:

                frame.addWindowListener(new WindowListener() {
                public void windowClosing(WindowEvent e) {
                    //Allows for multiple instances and properly closing
                    //only one of the Frames instead of all of them
                    frame.dispose();
                }
                public void windowOpened(WindowEvent e) {}              
                public void windowClosed(WindowEvent e) {}
                public void windowIconified(WindowEvent e) {}
                public void windowDeiconified(WindowEvent e) {}
                public void windowActivated(WindowEvent e) {}
                public void windowDeactivated(WindowEvent e) {}
            });
0

JFrame.DISPOSE_ON_CLOSE JFrame.EXIT_ON_CLOSE, EXIT_ON_CLOSE , . , . .

+3

Source: https://habr.com/ru/post/1735740/


All Articles