I created a JFrame which I named JMate. It should contain a button within a SettingsPanel, which when clicked, switches to PlayingPanel.
When you are in the PlayingPanel and try to close the frame by pressing on X button it should bring the SettingsPanel back up.

**Current Behaviour:**
When I launch the application, Frame with SettingsPanel opens.
When I press the button in SettingsPanel, SettingsPanele closes and PlayingPanele opens.
When I close PlayingPanel by Pressing on X, SettingsPanel opens for a few milliseconds and closes again.
The process than remains active but no Frame is visible...

I got it resolved but since I use CardLayout instead of 2 JFrames, the Problem now reoccured.

Why is this happening?

This is how my classes currently look:

```
public class JMate extends JFrame {
    public JPanel activePanel;
    public JPanel container;
    public JPanel settingsPanel;
    public JPanel playingPanel;
    public CardLayout cardLayout;

    public JMate(){
        setTitle("JMate v1.0.0");
        setLocationRelativeTo(null);
        setResizable(false);

        container = new JPanel();
        settingsPanel = new SettingsPanel(this);
        playingPanel = new PlayingPanel(this);

        cardLayout = new CardLayout();
        container.setLayout(cardLayout);

        container.add(settingsPanel, settingsPanel.getClass().getName());
        container.add(playingPanel, playingPanel.getClass().getName());

        cardLayout.show(container, settingsPanel.getClass().getName());
        activePanel = settingsPanel;

        add(container);
        setSize(settingsPanel.getPreferredSize());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                if(activePanel == settingsPanel){
                    System.exit(0);
                }
                else{
                    cardLayout.show(container, settingsPanel.getClass().getName());
                    setSize(settingsPanel.getPreferredSize());
                    activePanel = settingsPanel;
                    //TODO: stop engine etc.
                }
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel( new FlatDarkLaf() );
        } catch( Exception ex ) {
            System.err.println( "Failed to initialize LaF" );
        }
        EventQueue.invokeLater(JMate::new);
    }
}

public class SettingsPanel extends JPanel {
        private final JMate jMate;
        private SwitchButton switchButton;
        private EngineSelect engineSelect;
        private VariantSelect variantSelect;
        private JButton startButton;

        public SettingsPanel(JMate jMate) {
            this.jMate = jMate;
            setLayout(new FlowLayout());
            setPreferredSize(new Dimension(600,400));
            initComponents();
            setVisible(true);
        }

        private void initComponents(){
            switchButton = new SwitchButton();
            engineSelect = new EngineSelect();
            variantSelect = new VariantSelect();
            startButton = new JButton("Start");

            engineSelect.setVariantsSelect(variantSelect);
            variantSelect.setEngineSelect(engineSelect);

            add(switchButton);
            add(engineSelect);
            add(variantSelect);
            add(startButton);

            startButton.addActionListener(e -> {
                jMate.cardLayout.show(jMate.container, jMate.playingPanel.getClass().getName());
                jMate.setSize(jMate.playingPanel.getPreferredSize());
                jMate.activePanel = jMate.playingPanel;
                //TODO: start Engine etc.
            });
        }

    }

public class PlayingPanel extends JPanel {

private final JMate jMate;
public PlayingPanel(JMate jMate){
    this.jMate = jMate;
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(300,600));
    initComponents();
    setVisible(true);
}
private void initComponents() {

}

```

}